ES6 Features

Sraban Pahadasingh    June 24, 2024 07:24 AM

Below is a comprehensive list of these features: ES6 = ECMAScript 2015

1. Arrow Functions

  • Syntactic sugar for anonymous functions with a concise syntax.
  • Does not have its own this context, which is lexically scoped.

    const add = (a, b) => a + b;

    2. Block-Scoped Variables: let and const

  • let: Allows block-scoped variables.
  • const: Declares block-scoped constants that cannot be reassigned.

      let variable = "mutable";
      const constant = "immutable";

    3. Default Parameters

  • Allows function parameters to have default values.

    function greet(name = "World") {
        return `Hello, ${name}`;
    }

4. Template Literals

  • Multi-line strings and string interpolation using backticks (`).

    const name = "John";
    const greeting = `Hello, ${name}!`;

5. Destructuring Assignment

  • Extract values from arrays or properties from objects into distinct variables.

    const [x, y] = [1, 2];
    const { name, age } = { name: "Alice", age: 25 };

6. Rest and Spread Operators

  • Rest: Collects arguments into an array.
  • Spread: Expands elements of an array or object.

    const sum = (...args) => args.reduce((a, b) => a + b, 0); // Rest
    const arr = [1, 2, 3];
    const newArr = [...arr, 4, 5]; // Spread

7. Enhanced Object Literals

  • Shorthand property names, method definitions, and computed property names.

    const name = "Alice";
    const person = {
      name,
      greet() {
        console.log(`Hello, ${this.name}`);
      },
    };

8. Classes

  • Syntactic sugar for creating objects and handling inheritance.

    class Person {
      constructor(name) {
        this.name = name;
      }
      greet() {
        console.log(`Hello, ${this.name}`);
      }
    }

9. Modules (import/export)

  • Official support for module loading with import and export keywords.

    // module.js
    export const name = "Alice";
    
    // main.js
    import { name } from './module.js';

10. Promises

  • Asynchronous operations management with then, catch, and finally.

    const promise = new Promise((resolve, reject) => {
      setTimeout(() => resolve("Done"), 1000);
    });
    
    promise.then(result => console.log(result));

11. Block-Scoped Functions

  • Functions defined in a block are scoped to that block.

    if (true) {
      function sayHi() {
        console.log("Hi");
      }
      sayHi();
    }

12. Generators

  • Functions that can be paused and resumed, maintaining their own state.

    function* generator() {
    yield 1;
    yield 2;
    return 3;
    }

13. New Built-in Methods

  • New methods for various objects like Array, String, Object, etc.

    const str = "hello";
    str.includes("e"); // true
    
    const arr = Array.from("hello");

14. New Collections: Map, Set, WeakMap, WeakSet

  • Map: Key-value pairs.
  • Set: Unique values.
  • WeakMap and WeakSet: Garbage-collection friendly collections.

    const map = new Map();
    map.set('key', 'value');
    
    const set = new Set([1, 2, 2, 3]);

15. for...of Loop

  • Iterates over iterable objects like arrays, strings, etc.

    for (const item of [1, 2, 3]) {
      console.log(item);
    }

16. Binary and Octal Literals

  • Literal notations for binary (0b) and octal (0o) numbers.

    const binary = 0b1010;
    const octal = 0o755;

17. New Target Meta-Property

  • Provides a reference to the target of a function call.

    class Parent {
      constructor() {
        console.log(new.target.name);
      }
    }
    
    class Child extends Parent {
      constructor() {
        super();
      }
    }
    
    new Child(); // logs "Child"

18. New Math, Number, String, Object Methods

  • New methods for mathematical computations, number checking, string manipulation, and object handling.

    Number.isFinite(42); // true
    Math.trunc(4.9); // 4
    Object.assign({}, {a: 1}, {b: 2}); // {a: 1, b: 2}

19. Reflect API

  • Provides methods for interceptable JavaScript operations, similar to the proxy handlers.

    const obj = { x: 1 };
    Reflect.set(obj, 'x', 2);

20. Proxy API

  • Used to define custom behavior for fundamental operations (e.g., property lookup, assignment).

    const handler = {
      get: (target, property) => {
        return property in target ? target[property] : 42;
      }
    };
    
    const proxy = new Proxy({}, handler);
    console.log(proxy.foo); // 42

21. Symbol Type

  • A new primitive type for creating unique identifiers.

    const sym = Symbol("identifier");
    const obj = { [sym]: "value" };

In Summary:

Arrow functions, let and const, default parameters, template literals, destructuring assignment, rest and spread operators, enhanced object literals, classes, modules (import/export), promises, block-scoped functions, generators, new built-in methods (String.prototype.includes, Array.from, etc.), new collections (Map, Set, WeakMap, WeakSet), for...of loop, binary and octal literals, new target meta-property, new Math, Number, String, Object methods, Reflect API, Proxy API, Symbol type.

  • declaration - let,const
  • data type(primitive, nonprimitive) - Symbol, binary, octal
  • operator -> ??, ...
  • enhanced object literal , template literal
  • conditions/loop -> (for of loop)
  • function - blocked scope, default params assignment
  • generators yeild
  • class - new.target.name
  • new methods - String, Number, Array, Object Prototype
  • new collection - Map, Set, WeekMap, WeekSet
  • module - export/import
  • API - Math, Proxy, Reflect

medium





Comments powered by Disqus