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 freeSample questions
Which of the following correctly declares a constant variable in JavaScript ES6?
- var PI = 3.14;
- let PI = 3.14;
- const PI = 3.14;
- 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.
What does the `async` keyword in ES6 signify?
- It declares a function that will always return a string
- It declares a function that will automatically pause execution until a Promise resolves
- It declares a function that implicitly returns a Promise
- 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.
Consider the code: `let x = 10; function outer() { let x = 20; function inner() { console.log(x); } inner(); } outer();`. What is logged?
- 10
- 20
- undefined
- 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.
What is the purpose of the `Symbol.iterator` method in ES6?
- To define how an object can be iterated over
- To create a new symbol
- To perform mathematical operations
- 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.
What is a WeakMap in ES6?
- A Map where all keys must be weak references
- A Map where keys are weakly held, allowing garbage collection if the key is no longer referenced elsewhere
- A Map that only stores weak references
- 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.