ES6+ features overview

Computer Science · JavaScript

Question 1 of 10

0

    Set method that adds a value to the collection

    Capital letters and accents are ignored.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    ES6+ features overview explained

    ES6, also called ECMAScript 2015, added many features that make JavaScript shorter and clearer, and the later annual editions added more. The most used include let and const, arrow functions, template literals, destructuring, spread and classes.

    Destructuring pulls values out of arrays and objects into named variables and accepts default values. The spread operator, three dots, expands an array or object into individual items. In a parameter list, the same three dots collect remaining arguments into an array.

    const { name, age = 30 } = { name: "Ada" };
    const [first, ...rest] = [1, 2, 3];
    const user = { name: "Ada" };
    const city = user?.address?.city ?? "unknown";
    console.log(name, age, first, rest.length, city);
    

    Optional chaining, written ?., reads a nested property only when the part before it is not null or undefined. The nullish operator, ??, supplies a fallback only for null or undefined, unlike ||, which also replaces 0 and the empty string. Build tools such as Babel rewrite newer syntax for older browsers.

    Worked example

    The object has no age, so the default 30 is used. [1, 2, 3] gives first = 1 and rest = [2, 3], so rest.length is 2. user.address is undefined, so the chain returns undefined and ?? substitutes unknown. The output is Ada 30 1 2 unknown.

    What you will practise

    100 flashcards. Type your answer and get instant feedback.

    Example questions

    • The block-scoped variable keyword
    • The constant keyword
    • The concise function syntax
    • The text-embedding syntax
    • The spread operator
    • Keyword that declares a class
    • Keyword that makes a value available to other modules
    • Keyword that brings values in from another module

    …and 92 more.