This quiz assesses your mastery of advanced JavaScript ES6+ features, including variable declarations, modern syntax, destructuring, modules, and classes. Test your deep understanding of nuanced language behaviors and subtle pitfalls.
Block Scope Variable Behavior
What will be logged by the following code snippet: 'let a = 5; if (true) { let a = 10; } console.log(a);'?
- 5
- 10
- undefined
- null
- ReferenceError
Const Assignment Error
What is the result of executing the following code: 'const obj = {}; obj = {a:1};'?
- The code throws a ReferenceError.
- Nothing happens; code runs silently.
- The code throws a TypeError.
- The object obj is reassigned successfully.
- It logs 'undefined'.
Template Literals and Multiline Strings
Which statement accurately describes template literals in JavaScript?
- They automatically escape special characters like
and .
- They require double quotes for multi-line support.
- They only allow single-line strings.
- They preserve line breaks and allow embedded expressions using ${}.
- They do not support variable interpolation.
Array Destructuring with Skipped Elements
Given 'const arr = [1, 2, 3, 4];' what is the value of 'b' after '[, b, ,] = arr;'?
- 3
- 2
- 1
- undefined
- null
Object Destructuring and Default Values
Given 'const {a, b = 7} = {a: 3};', what is the value of 'b'?
- undefined
- 3
- 7
- null
- NaN
Spread Operator in Function Calls
Which result is produced by the following code: 'function sum(x, y, z) { return x + y + z; } const nums = [1, 2, 3]; sum(...nums);'?
- undefined
- 123
- Error: Too many arguments
- [1,2,3]
- 6
Rest Parameters vs. Arguments Object
Which statement is true about rest parameters in ES6 functions?
- Rest parameters are always empty if no argument is provided.
- Rest parameters are accessed with 'arguments' inside the function.
- Rest parameters transform all arguments into a single string.
- Rest parameters are true arrays, unlike the arguments object.
- Rest parameters include arguments passed before the ...rest parameter.
Named vs Default Exports
Considering the module export syntax, which import statement correctly imports the default export from a module named 'library.js'?
- import myExport from './library.js';
- import default from './library.js';
- import { myExport } from './library.js';
- import { default } from './library.js';
- import * as myExport from './library.js';
Re-exporting Named Exports
Which syntax re-exports a named export 'foo' from './module.js' without importing it locally?
- export * as foo from './module.js';
- export * from './module.js' as foo;
- export { foo } from './module.js';
- export foo from './module.js';
- export foo as './module.js';
Class Field Declaration
In JavaScript ES6, which syntax correctly defines an instance field 'count' initialized to 0 inside a class?
- let count = 0;
- this.count = 0; outside constructor
- var count = 0;
- count = 0;
- public count = 0;
Method Binding in Classes
Which approach is necessary to ensure a class method retains the correct 'this' context when used as a callback?
- Bind the method to 'this' in the constructor using '.bind(this)'.
- No action needed; methods are always bound.
- Declare the method as a static method only.
- Prefix the method with 'auto' keyword.
- Use an arrow function in the callback call only.
Destructuring and Aliasing
Given 'const obj = { x: 1, y: 2 };' which syntax assigns the value of 'x' to a variable 'z'?
- const z = obj[y];
- const { z: x } = obj;
- const { z } = obj;
- const z = obj.x;
- const { x: z } = obj;
Shallow Copy with Spread Operator
Given 'const arr1 = [1, [2, 3]]; const arr2 = [...arr1]; arr2[1][0] = 42;', what is the value of 'arr1[1][0]' after execution?
- 42
- null
- 2
- [42, 3]
- undefined
Immutability and Const
Which statement is true about objects declared with const?
- Their reference cannot be reassigned but their contents can be mutated.
- const prevents both reassignment and mutation.
- They are deeply frozen and immutable.
- Their properties cannot be changed but child objects can be.
- They become read-only arrays.
Default Parameters and Undefined
For the function 'function add(a = 2, b = 3) { return a + b; }', what does 'add(undefined, 4)' return?
- 6
- 4
- 5
- 23
- NaN