ES6+ Questions with Answers

19 practice questions on ES6+ (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

const sq = x => x * x;
console.log(sq(4), (() => 7)());
  1. 16, undefined
  2. 8, 7
  3. 16, 7
  4. 16, 4

Answer: C. 16, 7

Explanation

Concise arrows return their expression: sq(4) = 16 and the IIFE returns 7.

Step-by-step solution

  1. sq(4) = 16.
  2. The IIFE returns 7.

Why the other options are wrong

  • A. The concise arrow returns 7.
  • B. sq squares its argument.
  • D. The immediately invoked arrow returns 7.

Question 2Easy

What is the output?

const o = {};
console.log(o.a?.b ?? "none");
  1. undefined
  2. TypeError
  3. none
  4. null

Answer: C. none

Explanation

o.a is undefined, so ?. short-circuits to undefined instead of throwing, and ?? then supplies the fallback.

Step-by-step solution

  1. Optional chaining (?.) safely accesses a property WITHOUT throwing an error, even if an earlier part of the chain is null or undefined - it simply short-circuits and evaluates to undefined instead.
  2. Here, o is an empty object {}, so o.a is undefined; the chain o.a?.b then short-circuits (since o.a is undefined) and evaluates to undefined, WITHOUT attempting to read .b off of undefined (which would otherwise throw a TypeError).
  3. The nullish coalescing operator (??) then checks if that result (undefined) is null or undefined, and since it is, it substitutes the fallback value "none" instead.

Why the other options are wrong

  • A. undefined (left bare, unhandled) would be the result of o.a?.b ALONE, but the ?? "none" part specifically catches that undefined/null result and substitutes the fallback string instead.
  • B. A TypeError would occur WITHOUT optional chaining (if you wrote o.a.b directly), since accessing .b on undefined normally throws; but ?. specifically prevents that error by short-circuiting early.
  • D. null is a different specific value from undefined; here, o.a is undefined (a property that simply doesn't exist), and ?? treats both null and undefined the same way by substituting the fallback, but the actual intermediate value here is undefined, not null.

Tip: ?. only short-circuits for null/undefined specifically; it does NOT protect against other 'falsy' issues like accessing a property on a number or boolean.

Question 3Easy

What is the output?

const name = 'Dev', n = 3;
console.log(`${name} has ${n * 2} items`);
  1. Dev has 6 items
  2. Dev has n * 2 items
  3. ${name} has ${n * 2} items
  4. Dev has 3 items

Answer: A. Dev has 6 items

Explanation

Template literals evaluate each ${} expression, so n * 2 becomes 6.

Step-by-step solution

  1. name is "Dev".
  2. n * 2 = 6.

Why the other options are wrong

  • B. ${} evaluates expressions.
  • C. Template literals interpolate expressions.
  • D. n * 2 is evaluated, giving 6.

Question 4Easy

What is the output?

const a = [1, 2];
const b = [...a, 3];
b.push(4);
console.log(a.length, b.length);
  1. 3, 4
  2. 2, 4
  3. 2, 2
  4. 4, 4

Answer: B. 2, 4

Explanation

The spread builds a new array, so pushing to b does not affect a.

Step-by-step solution

  1. b is a copy of a plus 3.
  2. push adds one more to b.
  3. Lengths: 2 and 4.

Why the other options are wrong

  • A. The spread copies, so a is unchanged.
  • C. b has more elements.
  • D. The spread creates a new array.

Question 5Medium

What is the output?

const m = new Map([['a', 1]]);
m.set('b', 2).set('a', 10);
console.log([...m.entries()], m.size);
  1. [['a', 10], ['b', 2]], 2
  2. [['a', 1], ['b', 2], ['a', 10]], 3
  3. [['a', 10], ['b', 2]], 3
  4. [['b', 2], ['a', 10]], 2

Answer: A. [['a', 10], ['b', 2]], 2

Explanation

set returns the map and replaces the value for an existing key without moving it.

Step-by-step solution

  1. a=1 initially.
  2. b added at the end.
  3. a updated to 10 in place.
  4. Size 2.

Why the other options are wrong

  • B. Setting an existing key updates it.
  • C. Size counts distinct keys, which is 2.
  • D. Existing keys keep their original position.

Question 6Medium

What is the output?

console.log(0 || "x", 0 ?? "x");
  1. 0 0
  2. x x
  3. x 0
  4. 0 x

Answer: C. x 0

Explanation

|| falls back for any falsy value, including 0. ?? falls back only for null or undefined, so 0 is kept.

Step-by-step solution

  1. The logical OR operator (||) returns its right-hand side whenever the left-hand side is ANY 'falsy' value - which includes 0, "", NaN, null, undefined, and false, not just null/undefined.
  2. Here, 0 || "x" treats 0 as falsy (even though 0 is a perfectly valid, meaningful number), so it falls back to "x".
  3. The nullish coalescing operator (??), by contrast, falls back ONLY when the left-hand side is specifically null or undefined - since 0 is neither of those, 0 ?? "x" simply keeps and returns 0 itself.

Why the other options are wrong

  • A. "0 0" would require BOTH operators to preserve the 0, but || specifically treats 0 as falsy and replaces it with the fallback "x" instead.
  • B. "x x" would require BOTH operators to replace 0 with the fallback, but ?? specifically does NOT treat 0 as a reason to fall back, since 0 is neither null nor undefined.
  • D. "0 x" reverses which operator keeps 0 and which replaces it; it's || that discards 0 (treating it as falsy), while ?? is the one that correctly preserves it.

Tip: Use ?? specifically when 0, "", or false should be considered valid, meaningful values that should NOT trigger a fallback; use || only when you genuinely want ANY falsy value replaced.

Question 7Medium

What is the output?

function f(a, b = a * 2, ...c) { return [a, b, c.length]; }
console.log(f(1), f(1, 5, 6, 7));
  1. [1, 2, 0], [1, 5, 2]
  2. [1, undefined, 0], [1, 5, 2]
  3. [1, 2, 0], [1, 5, 3]
  4. [1, 2, 1], [1, 5, 2]

Answer: A. [1, 2, 0], [1, 5, 2]

Explanation

Defaults can use earlier params: f(1) gives b = 2, and in the second call rest holds 6 and 7.

Step-by-step solution

  1. f(1): b = 2, rest empty.
  2. f(1, 5, 6, 7): b = 5, rest = [6, 7].

Why the other options are wrong

  • B. b defaults to a * 2.
  • C. Rest receives only extra arguments after b.
  • D. Rest is empty in the first call.

Question 8Medium

What is the output?

const a = [3, 1, 2];
const b = a.toSorted();
console.log(a.join(), b.join());
  1. 1,2,3 1,2,3
  2. 3,1,2 3,1,2
  3. 3,1,2 1,2,3
  4. TypeError

Answer: C. 3,1,2 1,2,3

Explanation

toSorted (ES2023) returns a new sorted array and leaves the original unchanged, unlike sort which sorts in place.

Step-by-step solution

  1. Array.prototype.sort() is an older method that sorts the array IN PLACE, meaning it mutates and permanently reorders the original array itself, while also returning that same (now-reordered) array.
  2. toSorted() (added in ES2023) is a newer, non-mutating counterpart: it returns a brand-new, separately-sorted array WITHOUT touching or reordering the original array at all.
  3. Here, a.toSorted() creates b as a new sorted array ([1,2,3]), while the original array a remains completely untouched, still in its original order ([3,1,2]).

Why the other options are wrong

  • A. "1,2,3 1,2,3" would require toSorted() to ALSO mutate and reorder the original array a, which it specifically does NOT do - that mutating behaviour belongs to the older .sort() method instead.
  • B. "3,1,2 3,1,2" would mean toSorted() returned an unsorted copy (failing to sort at all), but toSorted() DOES correctly sort its returned copy; it just doesn't touch the original array a.
  • D. A TypeError isn't triggered; toSorted() is a standard, valid array method in modern JavaScript engines, and calling it here is perfectly correct syntax.

Tip: When you need to keep the original array unchanged, prefer the newer toSorted()/toReversed()/toSpliced() over the older, mutating sort()/reverse()/splice().

Question 9Medium

What is the output?

const { a = 5, b = 2 } = { a: undefined, b: null };
console.log(a, b);
  1. undefined 2
  2. undefined null
  3. 5 2
  4. 5 null

Answer: D. 5 null

Explanation

Destructuring defaults apply only when the value is undefined. null is a real value, so b stays null.

Step-by-step solution

  1. Destructuring default values (like const { a = 5 } = obj) apply ONLY when the corresponding property's value is exactly undefined - not for any other 'empty-ish' value.
  2. Here, a is explicitly set to undefined in the source object, so its default value (5) DOES kick in, making a equal 5.
  3. b, however, is explicitly set to null (a real, deliberate value, not 'missing'), so its default (2) does NOT apply, and b stays exactly null.

Why the other options are wrong

  • A. "undefined 2" would mean a's default failed to apply (wrong, since a IS undefined and so the default DOES kick in) while b's default wrongly DID apply (also wrong, since null doesn't trigger defaults).
  • B. "undefined null" wrongly suggests a's default never applied at all, but destructuring defaults specifically exist to replace exactly this case: a value that is strictly undefined.
  • C. "5 2" wrongly assumes BOTH properties get their defaults, but defaults only trigger for undefined specifically, and b's actual value, null, is a distinct, 'real' value that intentionally bypasses the default.

Tip: Destructuring/parameter defaults trigger ONLY on undefined - remember this is narrower than falsy, and different from the ?? operator's null-or-undefined check.

Question 10Hard

What is the output?

console.log([1, 2, 3, 4].findLast(n => n % 2 === 1), [1, 2, 3].at(-1));
  1. 1 3
  2. 3 3
  3. 1 1
  4. 3 1

Answer: B. 3 3

Explanation

findLast searches from the end and returns the last odd element (3). at(-1) returns the last element (3).

Step-by-step solution

  1. Array.prototype.findLast() searches an array starting from the END and moving backward, returning the first element (in that reverse search direction) that satisfies the given test function.
  2. For [1, 2, 3, 4], searching backward for an odd number (n % 2 === 1) first encounters 3 (checking 4, then 3), so findLast returns 3.
  3. Array.prototype.at(-1) retrieves the element at a given index, where negative indices count backward from the end of the array; at(-1) specifically always means 'the last element', which for [1, 2, 3] is 3.

Why the other options are wrong

  • A. "1 3" wrongly gives 1 as the findLast result, which would actually be the result of the OLDER .find() method (searching from the START, finding the first odd number); findLast specifically searches from the end instead.
  • C. "1 1" similarly gets the findLast part wrong, and ALSO wrongly computes at(-1) as if it meant 'the first element' rather than 'the last element'.
  • D. "3 1" correctly identifies findLast's result (3) but wrongly computes at(-1) as the FIRST element (1) instead of the actual LAST element, which is what negative index -1 specifically refers to.

Tip: at(-1) is a clean, readable shortcut for 'the last element' - no more need to write arr[arr.length - 1].

Practise all 19 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics