ES6 vs. ES5: A Feature Showdown
1. Variable Declaration (ES6 vs. ES5)
ES6 vs. ES5: A Feature Showdown

ES6 for the Win: Modern Features Explained
1. Variable Declaration (ES6 vs. ES5)
// ES6 (let & const for block-level scope and prevent re-assignment)
let message = "Hello from ES6!";
const PI = 3.14159;
// ES5 (var for function-level scope and allows re-assignment)
var greeting = "Hello from ES5!";
greeting = "Goodbye"; // Allowed in ES5
2. Arrow Functions (ES6)
// ES6 (concise syntax for functions)
const multiply = (a, b) => a * b;
const greet = name => `Hello, ${name}!`; // Template literal usage
// ES5 (function keyword)
function multiply(a, b) {
return a * b;
}
function greet(name) {
return `Hello, ${name}!`;
}
3. Template Literals (ES6)
// ES6 (string interpolation with backticks)
const fullName = `John ${"Doe"}`;
const message = `The answer is ${2 + 2}.`;
// ES5 (string concatenation)
const fullName = "John " + "Doe";
const message = "The answer is " + (2 + 2) + ".";
4. Key/Property Shorthand (ES6)
// ES6 (shorter object literal syntax)
const name = "Alice";
const age = 30;
const person = { name, age }; // Implicitly assigns values from variables to keys
// ES5 (explicit object literal syntax)
const person = {
name: name,
age: age
};
5. Method Definition Shorthand (ES6)
// ES6 (concise syntax for object methods)
const obj = {
greet() {
console.log("Hello!");
}
};
// ES5 (function keyword for methods)
const obj = {
greet: function() {
console.log("Hello!");
}
};
6. Destructuring (ES6)
// ES6 (extracting values from arrays or objects)
const numbers = [1, 2, 3];
const [x, y] = numbers; // x = 1, y = 2
const person = { name: "Bob", age: 40 };
const { name, age: userAge } = person; // name = "Bob", userAge = 40
// ES5 (traditional assignment)
const numbers = [1, 2, 3];
const x = numbers[0];
const y = numbers[1];
const person = { name: "Bob", age: 40 };
const name = person.name;
const userAge = person.age;
7. Array Iteration (ES6)
// ES6 (for...of loop for iterating over elements)
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
// ES5 (traditional for loop with index)
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
8. Default Parameters (ES6)
// ES6 (assigning default values to function parameters)
function greet(name = "World") {
console.log(`Hello, ${name}!`);
}
// ES5 (checking for undefined and assigning default value)
function greet(name) {
if (name === undefined) {
name = "World";
}
console.log(`Hello, ${name}!`);
}
9. Spread Syntax (ES6)
// ES6 (... operator for spreading elements)
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const combined = [...numbers1, ...numbers2]; // combined = [1, 2, 3, 4, 5, 6]
// ES5 (apply method for function arguments)
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const combined = numbers1.concat(numbers2);
10. Modules (ES6 — export/import)
While ES5 relied on script tags or external files for code organization, ES6 introduced a modular system:
ES6:
// file1.js (export)
export const message = "Hello from ES6 module!";
// file2.js (import)
import { message } from './file1.js';
console.log(message); // Output: "Hello from ES6 module!"
ES5 (Simulating Modules with IIFEs):
// file1.js (IIFE)
(function() {
const message = "Hello from ES5 simulated module!";
window.message = message; // Expose to global scope
})();
// file2.js
console.log(window.message); // Output: "Hello from ES5 simulated module!"
11. Classes/Constructor Functions (ES6)
ES6 introduced classes, a syntactic sugar over constructor functions for object-oriented programming:
ES6:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}!`);
}
}
const person1 = new Person("John", 30);
person1.greet(); // Output: "Hello, my name is John!"
ES5:
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name}!`);
};
const person1 = new Person("John", 30);
person1.greet(); // Output: "Hello, my name is John!"
Promises vs. Callbacks (ES6 — Promises, ES5 — Callbacks)
Callbacks (ES5):
Callbacks are functions passed as arguments to other functions. They are a common way to handle asynchronous operations in ES5. The function performing the asynchronous operation (e.g., making a network request) takes a callback as an argument. When the operation completes, the asynchronous function calls the callback function, often passing any results or errors as arguments.
// ES5 (Callback)
function fetchData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, JSON.parse(xhr.responseText)); // Pass null for error, data for success
} else {
callback(new Error('Failed to fetch data'), null); // Pass error, null for success
}
};
xhr.send();
}
fetchData('https://api.example.com/data', (error, data) => {
if (error) {
console.error(error);
} else {
console.log(data);
}
});
Challenges with Callbacks:
- Callback Hell: Nesting callbacks can lead to deeply indented code that becomes difficult to read and maintain.
- Error Handling: Can be cumbersome to manage errors passed through multiple callbacks.
- Asynchronous Flow Control: Difficult to write code that executes in a specific order after multiple asynchronous operations.
Promises (ES6):
Promises provide a cleaner and more structured way to handle asynchronous operations. A promise is an object representing the eventual completion (or failure) of an asynchronous operation. It has three states:
- Pending: The initial state while the operation is ongoing.
- Fulfilled: The operation completed successfully.
- Rejected: The operation encountered an error.
// ES6 (Promise)
function fetchData(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(new Error('Failed to fetch data'));
}
};
xhr.send();
});
}
fetchData('https://api.example.com/data')
.then(data => console.log(data)) // Handle successful response
.catch(error => console.error(error)); // Handle error
Advantages of Promises:
- Improved Readability: Code is cleaner and easier to follow due to linear chaining of
.then()and.catch()methods. - Error Handling: Errors are propagated through the promise chain, making it easier to catch and handle them.
- Asynchronous Flow Control: Promises enable chaining asynchronous operations using
.then()and.catch()methods, allowing for a more defined execution order.
In summary, while callbacks were the primary way to handle asynchronous operations in ES5, promises (introduced in ES6) offer a more structured, readable, and maintainable approach for modern JavaScript development.
메타데이터
- post_id
- cc744a7b8f79
- slug
- es6-vs-es5-a-feature-showdown-cc744a7b8f79
- url
- https://medium.com/@louistrinh/es6-vs-es5-a-feature-showdown-cc744a7b8f79
- canonical_url
- https://medium.com/@louistrinh/es6-vs-es5-a-feature-showdown-cc744a7b8f79
- author_url
- https://medium.com/@louistrinh
- status
- ok
- fetched_at
- 2026-06-11 05:11:55