ECMAScript 2015, commonly known as ES6, was a significant update to JavaScript, introducing many new features and syntactic improvements. It was released in June 2015 and marked a major milestone in the language’s evolution.
Key Features
- let and const: Block-scoped variable declarations.
- Arrow Functions: Concise syntax for writing function expressions.
- Classes: Syntactical sugar for object-oriented programming.
- Template Literals: Enhanced way to work with strings.
- Destructuring: Easily extract values from arrays or properties from objects.
- Default Parameters: Ability to set default values for function parameters.
- Rest and Spread Operators: Improved way to handle function arguments and array manipulation.
- Promises: Built-in support for handling asynchronous operations.
- Modules: Native support for modular JavaScript code.
- Map and Set: New data structures for better performance in certain scenarios.
Examples
// Arrow functions and template literals
const greet = name => `Hello, ${name}!`;
console.log(greet('World')); // "Hello, World!"
// Destructuring and default parameters
const getFullName = ({ firstName, lastName = 'Doe' }) => `${firstName} ${lastName}`;
console.log(getFullName({ firstName: 'John' })); // "John Doe"
// Classes
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.height * this.width;
}
}
const rect = new Rectangle(5, 8);
console.log(rect.area); // 40
// Promises
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('Data received'), 1000);
});
};
fetchData().then(data => console.log(data));
// Modules (in separate files)
// math.js
export const sum = (a, b) => a + b;
// main.js
import { sum } from './math.js';
console.log(sum(5, 3)); // 8
ES6 significantly modernized JavaScript, making it more powerful and expressive. Many of these features are now considered essential in modern JavaScript development.