This is Part 8 of 10, a bonus practice article with 70 code-output challenges. Each challenge asks you to predict the result before revealing the answer and reasoning.
Complete series
This Dev.to series has five core handbook articles plus five focused practice extras. Open the series page to move through the complete reading order:
- Part 1: JavaScript — core handbook, questions 1–120
- Part 2: React — core handbook, questions 121–220
- Part 3: React Native — core handbook, questions 221–420
- Part 4: Performance & Architecture — core handbook, questions 421–560
- Part 5: Senior & System Design — core handbook, questions 561–719
- Part 6: Output-Based JavaScript Practice — bonus practice article
- Part 7: Coding Interview Practice — bonus practice article
- Part 8: Code Output Challenges — bonus practice article
- Part 9: Current React Native Interview Questions — new high-frequency practice article
- Part 10: Project & Production Interviews — senior project ownership and real-production practice
How to use this challenge set
Read the code, state the exact output or error, then explain the language rule. Do not run the snippet until you have committed to an answer. For React Native interviews, connect the JavaScript behavior to rendering, state updates, list handling, or the JavaScript thread when relevant.
Skills tested
- Hoisting, scope, closures, and
this - Arrays, conditions, references, object behavior, and loose versus strict equality
- Promises, timers,
async/await, and microtasks - Common JavaScript patterns used in React and React Native interviews
Code output challenges
Challenge 1. Block-scoped counter
Predict the exact output before opening the answer.
let total = 0;
for (let i = 0; i < 3; i++) {
total += i;
}
console.log(total);
Why: The loop adds 0, 1, and 2.Answer and explanation
Expected output: 3
Challenge 2. var callback loop
Predict the exact output before opening the answer.
for (var i = 0; i < 3; i++) {
setTimeout(() => {
console.log(i);
}, 0);
}
Why: Answer and explanation
Expected output: 3, 3, 3
var creates one shared function-scoped binding.
Challenge 3. let callback loop
Predict the exact output before opening the answer.
for (let i = 0; i < 3; i++) {
setTimeout(() => {
console.log(i);
}, 0);
}
Why: Answer and explanation
Expected output: 0, 1, 2
let creates a fresh binding for each iteration.
Challenge 4. Mutation through an array reference
Predict the exact output before opening the answer.
const a = [1, 2];
const b = a;
b.push(3);
console.log(a.length);
Why: Answer and explanation
Expected output: 3
a and b reference the same array.
Challenge 5. Shallow object copy
Predict the exact output before opening the answer.
const a = { user: { name: 'A' } };
const b = { ...a };
b.user.name = 'B';
console.log(a.user.name);
Why: Object spread copies only the outer object.Answer and explanation
Expected output: B
Challenge 6. Array map with a missing return
Predict the exact output before opening the answer.
console.log(
[1, 2].map((x) => {
x * 2;
}),
);
Why: A block-bodied arrow function needs an explicit Answer and explanation
Expected output: [undefined, undefined]
return.
Challenge 7. reduce accumulator
Predict the exact output before opening the answer.
console.log([1, 2, 3].reduce((sum, x) => sum + x, 0));
Why: The accumulator starts at zero and receives every value.Answer and explanation
Expected output: 6
Challenge 8. Default numeric sort
Predict the exact output before opening the answer.
console.log([10, 2, 1].sort());
Why: Without a comparator, values are sorted as strings.Answer and explanation
Expected output: [1, 10, 2]
Challenge 9. Sparse array hole
Predict the exact output before opening the answer.
const a = [1, , 3];
console.log(a.length, 1 in a);
Why: The missing element is a hole, but array length remains three.Answer and explanation
Expected output: 3, false
Challenge 10. filter(Boolean)
Predict the exact output before opening the answer.
console.log([0, 1, '', 2, null].filter(Boolean));
Why: Answer and explanation
Expected output: [1, 2]
Boolean removes every falsy value.
Challenge 11. Loose versus strict equality
Predict the exact output before opening the answer.
console.log(0 == false, 0 === false);
Why: Loose equality coerces types; strict equality does not.Answer and explanation
Expected output: true, false
Challenge 12. Nullish coalescing
Predict the exact output before opening the answer.
console.log(0 || 10, 0 ?? 10);
Why: Answer and explanation
Expected output: 10, 0
|| falls back for falsy values, while ?? only falls back for nullish values.
Challenge 13. Optional chaining
Predict the exact output before opening the answer.
const user = null;
console.log(user?.profile?.name ?? 'Guest');
Why: Optional chaining returns Answer and explanation
Expected output: Guest
undefined, then ?? supplies the fallback.
Challenge 14. Closure counter
Predict the exact output before opening the answer.
function make() {
let n = 0;
return () => ++n;
}
const next = make();
console.log(next(), next());
Why: The returned function retains its lexical Answer and explanation
Expected output: 1, 2
n binding.
Challenge 15. Arrow lexical this
Predict the exact output before opening the answer.
const user = {
name: 'A',
show() {
return (() => this.name)();
},
};
console.log(user.show());
Why: The arrow captures Answer and explanation
Expected output: A
this from the regular show method.
Challenge 16. Bound function receiver
Predict the exact output before opening the answer.
function show() {
return this.name;
}
const f = show.bind({ name: 'A' });
console.log(f());
Why: Answer and explanation
Expected output: A
bind creates a function with a fixed receiver.
Challenge 17. Promise before timer
Predict the exact output before opening the answer.
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
Why: Synchronous work runs first, then microtasks, then timer tasks.Answer and explanation
Expected output: A, D, C, B
Challenge 18. await continuation
Predict the exact output before opening the answer.
async function run() {
console.log(1);
await 0;
console.log(2);
}
run();
console.log(3);
Why: Code after Answer and explanation
Expected output: 1, 3, 2
await continues in a microtask.
Challenge 19. Promise error recovery
Predict the exact output before opening the answer.
Promise.reject('x')
.catch(() => 2)
.then(console.log);
Why: Returning from Answer and explanation
Expected output: 2
catch fulfills the next promise.
Challenge 20. Async map result
Predict the exact output before opening the answer.
const x = [1, 2].map(async (n) => n * 2);
console.log(x[0] instanceof Promise);
Why: An async callback always returns a Promise.Answer and explanation
Expected output: true
Challenge 21. Destructuring defaults
Predict the exact output before opening the answer.
const [a = 1, b = 2] = [undefined, null];
console.log(a, b);
Why: Defaults apply to Answer and explanation
Expected output: 1, null
undefined, not null.
Challenge 22. Object key coercion
Predict the exact output before opening the answer.
const o = {},
a = {},
b = {};
o[a] = 'one';
o[b] = 'two';
console.log(o[a]);
Why: Plain-object keys are coerced to the same string.Answer and explanation
Expected output: two
Challenge 23. Prototype lookup
Predict the exact output before opening the answer.
const parent = { role: 'admin' };
const user = Object.create(parent);
console.log(user.role);
Why: Property lookup follows the prototype chain.Answer and explanation
Expected output: admin
Challenge 24. Delete reveals prototype
Predict the exact output before opening the answer.
const p = { x: 1 },
o = Object.create(p);
o.x = 2;
delete o.x;
console.log(o.x);
Why: Deleting the own property reveals the inherited one.Answer and explanation
Expected output: 1
Challenge 25. Object.freeze is shallow
Predict the exact output before opening the answer.
const o = Object.freeze({ x: { n: 1 } });
o.x.n = 2;
console.log(o.x.n);
Why: The nested object is not frozen.Answer and explanation
Expected output: 2
Challenge 26. React-style direct updates
Predict the exact output before opening the answer.
let count = 0;
const setCount = (v) => {
count = v;
};
setCount(count + 1);
setCount(count + 1);
console.log(count);
Why: This plain JavaScript model evaluates each update immediately; React batching differs, so discuss that distinction in an interview.Answer and explanation
Expected output: 2
Challenge 27. Debounce timer replacement
Predict the exact output before opening the answer.
let id;
const debounce = (f) => (x) => {
clearTimeout(id);
id = setTimeout(() => f(x), 0);
};
const f = debounce(console.log);
f(1);
f(2);
Why: The second call clears the first pending timer.Answer and explanation
Expected output: 2
Challenge 28. Promise.all result order
Predict the exact output before opening the answer.
Promise.all([Promise.resolve(2), 1]).then(console.log);
Why: Answer and explanation
Expected output: [2, 1]
Promise.all preserves input order after resolving values.
Challenge 29. Function hoisting
Predict the exact output before opening the answer.
console.log(add(1, 2));
function add(a, b) {
return a + b;
}
Why: Function declarations are initialized during scope creation.Answer and explanation
Expected output: 3
Challenge 30. JSON clone limitation
Predict the exact output before opening the answer.
const a = { x: undefined };
const b = JSON.parse(JSON.stringify(a));
console.log('x' in b);
Why: JSON serialization drops Answer and explanation
Expected output: false
undefined object properties.
Hoisting, equality, and coercion challenges
These 40 additional challenges focus on the JavaScript traps frequently used in interview rounds: hoisting, scope, ==, ===, arrays, objects, coercion, and reference identity.
Hoisting and scope challenges
🟢 Beginner
Challenge 31. Hoisted var value
Predict the exact output or error before opening the answer.
console.log(score);
var score = 10;
Why: Answer and explanation
Expected output: undefined
var declarations are initialized as undefined before execution; the assignment happens later.
Challenge 32. Temporal Dead Zone with let
Predict the exact output or error before opening the answer.
console.log(score);
let score = 10;
Why: Answer and explanation
Expected output: ReferenceError
let exists in the Temporal Dead Zone until its declaration runs.
Challenge 33. Function declaration hoisting
Predict the exact output or error before opening the answer.
show();
function show() {
console.log('ready');
}
Why: Function declarations are initialized with their function body during scope creation.Answer and explanation
Expected output: ready
Challenge 34. Function expression hoisting
Predict the exact output or error before opening the answer.
show();
var show = function () {
console.log('ready');
};
Why: Only Answer and explanation
Expected output: TypeError
show is hoisted as undefined; it is not callable before assignment.
Challenge 35. Local var shadows outer state
Predict the exact output or error before opening the answer.
var value = 'outer';
function printValue() {
console.log(value);
var value = 'inner';
}
printValue();
Why: The local Answer and explanation
Expected output: undefined
var value is hoisted and shadows the outer variable.
Challenge 36. Block scope with const
Predict the exact output or error before opening the answer.
{
const token = 'secret';
}
console.log(typeof token);
Why: Answer and explanation
Expected output: undefined
const is block-scoped; typeof on an undeclared name returns undefined.
Challenge 37. Parameter and var binding
Predict the exact output or error before opening the answer.
function update(value) {
console.log(value);
var value = 'new';
console.log(value);
}
update('old');
Why: A parameter and a same-named Answer and explanation
Expected output: old, then new
var share one function-scoped binding.
🟡 Intermediate
Challenge 38. Default parameter self-reference
Predict the exact output or error before opening the answer.
let value = 1;
function read(value = value) {
return value;
}
console.log(read());
Why: The parameter binding shadows the outer name while it is still uninitialized.Answer and explanation
Expected output: ReferenceError
Challenge 39. Class declaration timing
Predict the exact output or error before opening the answer.
const user = new User();
class User {}
Why: Classes are not usable before their declaration is evaluated.Answer and explanation
Expected output: ReferenceError
Challenge 40. Named function expression scope
Predict the exact output or error before opening the answer.
const run = function internal() {
return typeof internal;
};
console.log(run());
console.log(typeof internal);
Why: The expression name is available inside the function body, not outside it.Answer and explanation
Expected output: function, then undefined
Challenge 41. var leaks from an if block
Predict the exact output or error before opening the answer.
if (true) {
var message = 'visible';
}
console.log(message);
Why: Answer and explanation
Expected output: visible
var has function scope rather than block scope.
Challenge 42. let does not leak from an if block
Predict the exact output or error before opening the answer.
if (true) {
let message = 'hidden';
}
console.log(typeof message);
Why: Answer and explanation
Expected output: undefined
let remains inside the block.
Challenge 43. Nested shadowing
Predict the exact output or error before opening the answer.
let count = 1;
{
let count = 2;
console.log(count);
}
console.log(count);
Why: The inner block creates a separate binding.Answer and explanation
Expected output: 2, then 1
Challenge 44. Closure reads the latest binding
Predict the exact output or error before opening the answer.
let status = 'loading';
const readStatus = () => console.log(status);
status = 'ready';
readStatus();
Why: Closures retain the binding, not a copied old value.Answer and explanation
Expected output: ready
🔴 Advanced
Challenge 45. for loop let scope
Predict the exact output or error before opening the answer.
for (let index = 0; index < 1; index++) {}
console.log(typeof index);
Why: The loop initializer Answer and explanation
Expected output: undefined
let does not escape the loop.
Challenge 46. for loop var scope
Predict the exact output or error before opening the answer.
for (var index = 0; index < 1; index++) {}
console.log(index);
Why: The Answer and explanation
Expected output: 1
var binding is available after the loop.
Challenge 47. Redeclaring let
Predict the exact output or error before opening the answer.
let value = 1;
let value = 2;
Why: A lexical binding cannot be redeclared in the same scope.Answer and explanation
Expected output: SyntaxError
Challenge 48. Reassigning const
Predict the exact output or error before opening the answer.
const value = 1;
value = 2;
Why: Answer and explanation
Expected output: TypeError
const prevents rebinding after initialization.
Challenge 49. Hoisted assignment order
Predict the exact output or error before opening the answer.
var name = 'first';
function read() {
console.log(name);
var name = 'second';
}
read();
Why: The function-local Answer and explanation
Expected output: undefined
var name shadows the outer value before its assignment.
Challenge 50. Function declaration replaced by assignment
Predict the exact output or error before opening the answer.
function getValue() {
return 1;
}
var getValue = 2;
console.log(typeof getValue);
Why: The later assignment replaces the function value at runtime.Answer and explanation
Expected output: number
Equality, coercion, arrays, and objects
🟡 Intermediate
Challenge 51. Array reference equality
Predict the exact output or error before opening the answer.
console.log([] === []);
Why: Each array literal creates a different object.Answer and explanation
Expected output: false
Challenge 52. Array and false
Predict the exact output or error before opening the answer.
console.log([] == false);
Why: The empty array becomes an empty string, then zero; Answer and explanation
Expected output: true
false also becomes zero.
Challenge 53. Array and zero
Predict the exact output or error before opening the answer.
console.log([] == 0);
Why: Loose equality converts the empty array to an empty string and then to zero.Answer and explanation
Expected output: true
Challenge 54. Array and empty string
Predict the exact output or error before opening the answer.
console.log([] == '');
Why: An empty array converts to an empty string.Answer and explanation
Expected output: true
Challenge 55. Single zero array and zero
Predict the exact output or error before opening the answer.
console.log([0] == 0);
Why: Answer and explanation
Expected output: true
[0] converts to the string "0", then to numeric zero.
Challenge 56. Single one array and true
Predict the exact output or error before opening the answer.
console.log([1] == true);
Why: Answer and explanation
Expected output: true
[1] converts to "1"; both sides then compare as numeric one.
Challenge 57. Array string conversion
Predict the exact output or error before opening the answer.
console.log([1, 2] == '1,2');
Why: Arrays convert to their comma-joined string representation for this comparison.Answer and explanation
Expected output: true
Challenge 58. Array versus object
Predict the exact output or error before opening the answer.
console.log([] == {});
Why: The array becomes Answer and explanation
Expected output: false
"" while the object becomes "[object Object]".
Challenge 59. Object reference equality
Predict the exact output or error before opening the answer.
console.log({} === {});
Why: Object equality compares identity, not matching properties.Answer and explanation
Expected output: false
Challenge 60. Null and undefined
Predict the exact output or error before opening the answer.
console.log(null == undefined, null === undefined);
Why: Loose equality has a special null/undefined rule; strict equality requires identical types.Answer and explanation
Expected output: true, false
Challenge 61. Null and zero
Predict the exact output or error before opening the answer.
console.log(null == 0);
Why: The null/undefined loose-equality exception does not include zero.Answer and explanation
Expected output: false
Challenge 62. Empty string and zero
Predict the exact output or error before opening the answer.
console.log('' == 0, '' === 0);
Why: Loose equality coerces the empty string to zero; strict equality does not coerce.Answer and explanation
Expected output: true, false
Challenge 63. False and string zero
Predict the exact output or error before opening the answer.
console.log(false == '0');
Why: Both operands coerce to numeric zero during loose equality.Answer and explanation
Expected output: true
Challenge 64. NaN equality
Predict the exact output or error before opening the answer.
console.log(NaN === NaN, Object.is(NaN, NaN));
Why: Answer and explanation
Expected output: false, true
NaN is not equal to itself with ===; Object.is handles it specially.
Challenge 65. Object.is and signed zero
Predict the exact output or error before opening the answer.
console.log(0 === -0, Object.is(0, -0));
Why: Answer and explanation
Expected output: true, false
=== treats signed zeroes as equal; Object.is distinguishes them.
Challenge 66. Null array string conversion
Predict the exact output or error before opening the answer.
console.log([null] == '');
Why: A one-item array containing null converts to an empty string.Answer and explanation
Expected output: true
Challenge 67. Undefined array string conversion
Predict the exact output or error before opening the answer.
console.log([undefined] == '');
Why: A one-item array containing undefined also converts to an empty string.Answer and explanation
Expected output: true
Challenge 68. Boolean wrapper truthiness
Predict the exact output or error before opening the answer.
console.log(Boolean(new Boolean(false)));
Why: Wrapper objects are objects, and objects are truthy.Answer and explanation
Expected output: true
Challenge 69. Empty object truthiness
Predict the exact output or error before opening the answer.
console.log(Boolean({}), Boolean([]));
Why: All ordinary objects and arrays are truthy, including empty ones.Answer and explanation
Expected output: true, true
Challenge 70. Strict equality prevents coercion
Predict the exact output or error before opening the answer.
console.log('5' === 5, Number('5') === 5);
Why: Strict equality compares both type and value; explicit conversion makes types match.Answer and explanation
Expected output: false, true
Continue practising
Revisit Parts 6 and 7 for larger output-based and coding practice sets, then return to the core handbook for architecture, system design, and behavioral preparation.
Top comments (0)