
Almost every modern JavaScript codebase is full of ES6+ syntax. This set tests the edge cases: when destructuring defaults kick in, how ?? is different from ||, what Map and WeakMap give you over plain objects, how generators work, and how ES module imports behave.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 16 questions.
Topics covered in this set
- Destructuring with default values
- Spread, rest and swapping
- Nullish coalescing (??) and optional chaining (?.)
- Map, Set, WeakMap
- Generators and iterators
- ES modules, Object.freeze and structuredClone
JavaScript MCQs with answers and explanations
Q1. What is the value of b?
const { a, b = 2 } = { a: 1, b: undefined };
a) undefined
b) TypeError
c) 2
d) null
Q2. What is the value of b?
const { a, b = 2 } = { a: 1, b: null };
a) 2
b) 0
c) undefined
d) null
Q3. What is printed?
const x = 0 ?? 5;
const y = 0 || 5;
console.log(x, y);
a) 0 5
b) 0 0
c) 5 5
d) 5 0
Q4. What is printed?
const user = {};
console.log(user?.address?.city);
a) null
b) ''
c) TypeError
d) undefined
Q5. What is printed?
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b);
a) 1 2
b) 2 2
c) SyntaxError
d) 2 1
Q6. What does f(1, 2, 3) return?
function f(a, ...rest) {
return rest.length;
}
a) 2
b) 3
c) 1
d) undefined
Q7. What is printed?
console.log([...new Set([1, 1, 2, '2'])].length);
a) 3
b) 1
c) 2
d) 4
Q8. Which is an advantage of Map over a plain object for key-value data?
a) Map is always serialised by JSON.stringify
b) Map keys can only be strings
c) Map keys can be any value, including objects, and Map has a size property and keeps insertion order
d) Map has no iteration support
Q9. Which statement about ES module exports is correct?
a) A module can have one default export and any number of named exports
b) Modules cannot export functions
c) A module can have many default exports
d) Named exports must be imported with any name you like
Q10. What happens if a module does import { count } from './a.js' and then runs count = 5?
a) The value in a.js is updated
b) A new local variable is created
c) Nothing happens
d) A TypeError is thrown because imported bindings are read-only
Q11. What is printed?
const o = Object.freeze({ a: { b: 1 } });
o.a.b = 2;
console.log(o.a.b);
a) TypeError always
b) 1
c) undefined
d) 2
Q12. What is printed?
function* g() {
yield 1;
yield 2;
}
const it = g();
it.next();
console.log(it.next());
a) { value: 2, done: true }
b) { value: 2, done: false }
c) { value: undefined, done: true }
d) { value: 1, done: false }
Q13. What does [10, 20, 30].at(-1) return?
a) 10
b) undefined
c) TypeError
d) 30
Q14. You need a deep copy of an object that contains a Date and a Map. Which built-in works best?
a) Object.assign({}, obj)
b) { ...obj }
c) JSON.parse(JSON.stringify(obj))
d) structuredClone(obj)
Q15. Which statement about WeakMap is correct?
a) It has a size property
b) It is iterable with for...of
c) Its keys can be strings
d) Its keys must be objects (or non-registered symbols) and it does not stop them from being garbage collected
Q16. What is printed?
const prices = { a: 1, b: 2 };
const doubled = Object.fromEntries(
Object.entries(prices).map(([k, v]) => [k, v * 2])
);
console.log(doubled);
a) [2, 4]
b) { a: 2, b: 4 }
c) [['a', 2], ['b', 4]]
d) { a: 1, b: 2 }
Quick revision notes
- Destructuring defaults apply only for undefined, not null.
- ?? falls back only for null/undefined; || falls back for any falsy value.
- Spread copies and Object.freeze are both shallow; use structuredClone for deep copies.
- Map and Set keep insertion order and accept any key. WeakMap and WeakSet hold object keys weakly and cannot be looped over.
- ES module imports are live and read-only. A module has at most one default export.
Frequently asked questions
What is the difference between spread and rest?
Both use the same ... syntax. Spread opens up an array or object into separate items. Rest does the opposite: it gathers several items into one array or object (in parameters or destructuring).
When should I use Map instead of an object?
Use Map when keys are not plain strings, when you add and remove keys a lot, or when you need a dependable order and size. Use objects for fixed, record-like data and for JSON.
Is optional chaining supported in all browsers?
Yes, every current major browser supports ?. and ??. For older browsers you need a transpiler like Babel or the TypeScript compiler.
Comments
Post a Comment