Sample questions
Question 1Easy
What is the output?
console.log([1, 2, 3, 4].reduce((sum, n) => sum + n, 10));
- 24
- 20
- 1234
- 10
Answer: B. 20
Explanation
reduce starts from the initial value 10 and adds each element: 10 + 1 + 2 + 3 + 4 = 20.
Step-by-step solution
Array.prototype.reduce() processes an array from left to right, combining elements into a single accumulated value using a callback function.- The second argument to
reduce (here, 10) sets the STARTING value of the accumulator (sum), before any array elements are even processed. - So the computation proceeds as: 10 + 1 = 11, then 11 + 2 = 13, then 13 + 3 = 16, then 16 + 4 = 20, giving a final result of 20.
Why the other options are wrong
- A. 24 would come from counting the last element (4) twice, which does not happen here.
- C. "1234" (as a concatenated-looking value) would suggest the elements were joined as strings rather than summed numerically, but reduce here is doing numeric addition (sum + n), not string concatenation.
- D. 10 alone would be the result only if the array were empty (so the initial value is simply returned unchanged) or if the callback never actually added anything, neither of which is the case here.
Tip: Always double check reduce's STARTING value argument - a surprisingly common source of off-by-one-element bugs.
Question 2Easy
What is the output?
console.log([1, 2, 3].reduce((acc, x) => acc + x, 10));
- 6
- 16
- 10
- 13
Answer: B. 16
Explanation
reduce starts at 10 and adds 1, 2, 3 for 16.
Step-by-step solution
- acc starts at 10.
- 10+1+2+3 = 16.
Why the other options are wrong
- A. The initial value 10 is added too.
- C. The elements are still added to the initial value.
- D. All three elements are added.
Question 3Easy
What is the output?
const a = [1, 2, 3];
console.log(a.slice(1), a.splice(1, 1), a);
- [2, 3], [2], [1, 2, 3]
- [2, 3], [2], [1, 3]
- [2, 3], [1, 3], [1, 3]
- [1, 3], [2], [1, 3]
Answer: B. [2, 3], [2], [1, 3]
Explanation
slice returns [2, 3] without mutating, splice removes and returns [2], leaving a as [1, 3].
Step-by-step solution
- slice(1) = [2, 3].
- splice(1, 1) removes 2.
- a is now [1, 3].
Why the other options are wrong
- A. splice mutates a.
- C. splice returns the removed elements, not the remaining array.
- D. slice(1) returns from index 1.
Question 4Easy
What is the output?
const a = { x: 1 };
const b = a;
b.x = 2;
console.log(a.x);
- 1
- undefined
- TypeError
- 2
Answer: D. 2
Explanation
Objects are assigned by reference: a and b point to the same object.
Step-by-step solution
- In JavaScript, objects (unlike primitive values like numbers or strings) are assigned and passed around BY REFERENCE, not by value.
const b = a; does not create a brand-new, independent copy of the object; it simply makes b point to the exact SAME object in memory that a already points to.- So when
b.x = 2; modifies that shared object's x property, the change is visible through BOTH variable names, since they both reference the same underlying object - hence a.x also becomes 2.
Why the other options are wrong
- A. 1 would be correct only if a and b referred to two SEPARATE, independent objects, but const b = a; just copies the reference, not the object's contents.
- B. undefined would suggest the x property somehow got removed or renamed, but nothing in this code does that; x simply gets reassigned to a new value (2).
- C. A TypeError isn't triggered; b.x = 2 is a perfectly normal, valid property assignment on an object.
Tip: To actually get an independent copy of an object, use spread syntax ({...a}) for a shallow copy, or structuredClone(a) for a deep copy - never a plain = assignment.
Question 5Medium
What is the output?
const a = { n: { v: 1 } };
const b = { ...a };
b.n.v = 2;
console.log(a.n.v);
- 1
- TypeError
- undefined
- 2
Answer: D. 2
Explanation
Spread makes a shallow copy. b.n still points to the same nested object as a.n. Use structuredClone for a deep copy.
Step-by-step solution
- Object spread syntax (
{ ...a }) creates a SHALLOW copy: it copies the object's own top-level properties into a new object, but any property whose VALUE is itself an object is just copied BY REFERENCE, not deeply cloned. - Here,
a.n is itself an object ({v: 1}); after b = {...a}, b.n still points to the exact SAME nested object as a.n, not a separate copy of it. - So when
b.n.v = 2; modifies that shared nested object, a.n.v also reflects the change, since both a.n and b.n reference the same underlying object.
Why the other options are wrong
- A. 1 would be correct only if the nested object {v: 1} had also been deeply cloned, but spread syntax only copies one level deep, leaving nested objects shared by reference.
- B. A TypeError isn't triggered here; b.n.v = 2 is a perfectly normal property assignment on an existing nested object.
- C. undefined would suggest the v property got removed or that b.n itself were missing, neither of which happens; the nested object and its v property both still exist, just with an updated value that's visible from BOTH a and b.
Tip: For a true deep copy with no shared nested references, use structuredClone(obj) (or a dedicated deep-clone utility) instead of spread or Object.assign.
Question 6Medium
What is the output?
console.log([3, 1, 2].toSorted?.() ?? 'n/a', [3, 1, 2].sort((a, b) => b - a));
- [3, 1, 2], [1, 2, 3]
- [1, 2, 3], [1, 2, 3]
- [1, 2, 3], [3, 1, 2]
- [1, 2, 3], [3, 2, 1]
Answer: D. [1, 2, 3], [3, 2, 1]
Explanation
toSorted returns a new ascending array and the comparator b - a sorts descending.
Step-by-step solution
- toSorted copy: [1, 2, 3].
- b - a gives descending: [3, 2, 1].
Why the other options are wrong
- A. toSorted returns a sorted copy.
- B. The comparator sorts descending.
- C. The comparator sorts the second array in place.
Question 7Medium
What is the output?
const a = [1, 2, 3];
const b = a;
b.push(4);
console.log(a, a === b, [1] === [1]);
- [1, 2, 3, 4], true, false
- [1, 2, 3], true, true
- [1, 2, 3], false, false
- [1, 2, 3, 4], true, true
Answer: A. [1, 2, 3, 4], true, false
Explanation
b and a reference the same array; two separate array literals are never strictly equal.
Step-by-step solution
- b = a copies the reference.
- push affects both.
- Distinct literals differ.
Why the other options are wrong
- B. Different array literals are different objects.
- C. b references the same array as a.
- D. [1] and [1] are two separate arrays.
Question 8Medium
What is the output?
console.log([10, 1, 2].sort().join());
- 10,2,1
- 1,2,10
- 1,10,2
- 10,1,2
Answer: C. 1,10,2
Explanation
Without a compare function, sort converts elements to strings and compares them lexicographically, so "10" sorts before "2". Use sort((a, b) => a - b) for numbers.
Step-by-step solution
Array.prototype.sort(), when called with NO comparison function argument, converts every element to a STRING first and then compares those strings lexicographically (character by character, like dictionary order).- For
[10, 1, 2], the string forms are "10", "1", "2"; comparing them as text, "1" comes before "10" (since "1" is a prefix match that's shorter), and "10" comes before "2" (since the character '1' sorts before the character '2'). - So the sorted order (as strings) is "1", "10", "2", giving the joined result "1,10,2".
Why the other options are wrong
- A. "10,2,1" doesn't correspond to any consistent sort order, numeric or lexicographic, for these specific values.
- B. "1,2,10" would be the result of a proper NUMERIC sort (which is what most people actually want), but default .sort() with no comparator does lexicographic STRING comparison instead.
- D. "10,1,2" is simply the original, unsorted order; .sort() with no comparator does still reorder based on its (lexicographic) comparison rule, it just doesn't produce what you'd intuitively expect for numbers.
Tip: Always pass an explicit comparator for numeric sorting: arr.sort((a, b) => a - b) for ascending order.
Question 9Hard
What is the output?
const o = { a: 1 };
Object.defineProperty(o, 'h', { value: 2, enumerable: false });
console.log(Object.keys(o), JSON.stringify(o), o.h, 'h' in o);
- ['a'], {"a":1}, 2, true
- ['a', 'h'], {"a":1,"h":2}, 2, true
- ['a'], {"a":1}, undefined, false
- ['a'], {"a":1,"h":2}, 2, true
Answer: A. ['a'], {"a":1}, 2, true
Explanation
Non-enumerable properties are hidden from Object.keys and JSON.stringify but can still be read and tested with in.
Step-by-step solution
- h is non-enumerable.
- keys and JSON skip it.
- o.h and "h" in o still work.
Why the other options are wrong
- B. Non-enumerable keys are skipped by keys and JSON.
- C. The property still exists.
- D. JSON.stringify skips non-enumerable properties.
Question 10Hard
What is the output?
const counts = ['a', 'b', 'a', 'c', 'a'].reduce((m, x) => ({ ...m, [x]: (m[x] || 0) + 1 }), {});
console.log(counts);
- {a: 1, b: 1, c: 1}
- {a: 3, b: 1}
- {a: 3, b: 1, c: 1}
- [3, 1, 1]
Answer: C. {a: 3, b: 1, c: 1}
Explanation
Each step spreads the accumulator and increments the key, giving a: 3, b: 1, c: 1.
Step-by-step solution
- Start with {}.
- Each letter increments its count.
- a appears 3 times.
Why the other options are wrong
- A. a is counted every time it appears.
- B. c is counted too.
- D. reduce builds an object here.
Practise all 22 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 →