Any2Quiz
Computer Science / Languages / JavaScript

ES6+ Quiz - Languages JavaScript

Free practice quiz: ES6+ (Languages JavaScript). 56 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 56 questions free

Sample questions

  1. Which of the following correctly declares a constant variable in JavaScript ES6?

    1. var PI = 3.14;
    2. let PI = 3.14;
    3. const PI = 3.14;
    4. constant PI = 3.14;
    Show answer

    Answer: C. const PI = 3.14;

    In ES6, the `const` keyword is used to declare variables whose values cannot be reassigned. `var` and `let` declare variables that can be reassigned, and `constant` is not a valid JavaScript keyword for variable declaration.

  2. What does the `async` keyword in ES6 signify?

    1. It declares a function that will always return a string
    2. It declares a function that will automatically pause execution until a Promise resolves
    3. It declares a function that implicitly returns a Promise
    4. It declares a function that is asynchronous by nature
    Show answer

    Answer: C. It declares a function that implicitly returns a Promise

    An `async` function always returns a Promise. If the function returns a non-Promise value, it's wrapped in a resolved Promise. If it throws an error, it returns a rejected Promise.

  3. Consider the code: `let x = 10; function outer() { let x = 20; function inner() { console.log(x); } inner(); } outer();`. What is logged?

    1. 10
    2. 20
    3. undefined
    4. Error
    Show answer

    Answer: B. 20

    JavaScript uses lexical scoping. The `inner` function is within the scope of `outer`, which has its own `x` variable with the value `20`. Therefore, `inner` accesses `x` from `outer`'s scope, not the global scope.

  4. What is the purpose of the `Symbol.iterator` method in ES6?

    1. To define how an object can be iterated over
    2. To create a new symbol
    3. To perform mathematical operations
    4. To handle asynchronous operations
    Show answer

    Answer: A. To define how an object can be iterated over

    The `Symbol.iterator` is a method that is called when a `for...of` loop or other iterable consumption methods are used on an object. It returns an iterator object, which implements the iterator protocol.

  5. What is a WeakMap in ES6?

    1. A Map where all keys must be weak references
    2. A Map where keys are weakly held, allowing garbage collection if the key is no longer referenced elsewhere
    3. A Map that only stores weak references
    4. A Map that throws an error if a key is too large
    Show answer

    Answer: B. A Map where keys are weakly held, allowing garbage collection if the key is no longer referenced elsewhere

    Unlike a `Map`, keys in a `WeakMap` must be objects and are held weakly. If an object used as a key is garbage collected, the entry in the `WeakMap` is automatically removed.

Take the full quiz

More quizzes

Browse all JavaScript quizzes