JavaScript ES6 (ECMAScript 2015)

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

  1. let and const: Block-scoped variable declarations.
  2. Arrow Functions: Concise syntax for writing function expressions.
  3. Classes: Syntactical sugar for object-oriented programming.
  4. Template Literals: Enhanced way to work with strings.
  5. Destructuring: Easily extract values from arrays or properties from objects.
  6. Default Parameters: Ability to set default values for function parameters.
  7. Rest and Spread Operators: Improved way to handle function arguments and array manipulation.
  8. Promises: Built-in support for handling asynchronous operations.
  9. Modules: Native support for modular JavaScript code.
  10. 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.