Can You Guess These 50 JavaScript Outputs? (React Native Interview Edition)
Can You Guess These 50 JavaScript Outputs? (React Native Interview Edition) React Native interviews rarely ask you to recite API docs. They drop a short snippet and ask: Guess the output? Same language. Same traps.
Can You Guess These 50 JavaScript Outputs? (React Native Interview Edition)
React Native interviews rarely ask you to recite API docs.
They drop a short snippet and ask:
Guess the output?
Same language. Same traps. Whether the code runs in a browser, Metro, or Hermes β JavaScript rules donβt change.
These 50 challenges cover what interviewers actually probe:
hoisting Β· scope Β· closures Β· this Β· coercion Β· references Β· prototypes Β· Promises Β· async/await Β· event loop
How to play (same as the Instagram / LinkedIn challenges)
- Read the snippet.
- Pick A / B / C / D before you scroll.
- Open π Reveal answer only after you commit.
- Say why out loud β interviewers care about the explanation as much as the letter.
Challenge rule: Do not run the code until you have made your prediction.
React Native note: Timers, Promises, closures, and shared object references show up as real bugs β stale state, double renders, wrong list keys, race conditions. The UI layer does not rewrite the language.
π₯ Warm-up: coercion, scope, and hoisting
β‘ Challenge 1 β Guess the output?
console.log(1 + "2" + 3 - 4);
A. 119
B. 1234
C. 1199
D. NaN
A.
119
1 + "2" + 3 β "123" (string concat).
"123" - 4 coerces to number β 119.
β‘ Challenge 2 β Guess the output?
console.log(score);
var score = 10;
A. 10
B. undefined
C. ReferenceError
D. null
B.
undefined
var is hoisted and initialized as undefined. Only the assignment stays in place.
β‘ Challenge 3 β Guess the output?
console.log(score);
let score = 10;
A. 10
B. undefined
C. ReferenceError
D. null
C.
ReferenceError
let is in the Temporal Dead Zone until its line runs. Classic RN interview follow-up after Challenge 2.
β‘ Challenge 4 β Guess the output?
console.log(add(2, 3));
function add(a, b) {
return a + b;
}
A. 5
B. undefined
C. TypeError
D. ReferenceError
A.
5
Function declarations are hoisted with their full body.
β‘ Challenge 5 β Guess the output?
console.log(add(2, 3));
var add = function (a, b) {
return a + b;
};
A. 5
B. undefined
C. TypeError
D. ReferenceError
C.
TypeError
add is hoisted as undefined, then you call undefined(...).
β‘ Challenge 6 β Guess the output?
console.log(name);
const name = "Amit";
A. "Amit"
B. undefined
C. ReferenceError
D. TypeError
C.
ReferenceError
Same TDZ rules as let.
β‘ Challenge 7 β Guess the output?
var a = 1;
if (true) {
var a = 2;
let b = 3;
}
console.log(a, typeof b);
A. 1 "undefined"
B. 2 "undefined"
C. 2 "number"
D. Error
B.
2 "undefined"
var is function-scoped (rewrites outer a).
b is block-scoped; typeof safely returns "undefined" for an undeclared identifier.
π Closures & loops (stale-timer territory)
β‘ Challenge 8 β Guess the output?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
A. 0 1 2
B. 3 3 3
C. 0 0 0
D. Error
B.
3 3 3
One shared var i. Loop finishes (i === 3) before timers fire.
Same class of bug as firing three API calls from a loop with the wrong index.
β‘ Challenge 9 β Guess the output?
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
A. 0 1 2
B. 3 3 3
C. 0 0 0
D. Error
A.
0 1 2
let creates a new binding per iteration.
β‘ Challenge 10 β Guess the output?
function makeCounter() {
let count = 0;
return () => ++count;
}
const counter = makeCounter();
console.log(counter(), counter());
A. 1 1
B. 0 1
C. 1 2
D. Error
C.
1 2
The returned function closes over the same count. Think custom hooks / factory helpers.
β‘ Challenge 11 β Guess the output?
function createCounter() {
let count = 0;
return () => ++count;
}
const a = createCounter();
const b = createCounter();
console.log(a(), a(), b());
A. 1 2 1
B. 1 2 3
C. 1 1 1
D. 0 1 0
A.
1 2 1
Each factory call gets its own lexical environment.
β‘ Challenge 12 β Guess the output?
let x = 5;
console.log(x++);
console.log(++x);
console.log(x);
A. 5, 6, 6
B. 5, 7, 7
C. 6, 6, 7
D. 6, 7, 7
B.
5, 7, 7
x++ returns then increments (5, now 6).
++x increments then returns (7).
Final x is 7.
π― this binding (methods, bind, arrows)
β‘ Challenge 13 β Guess the output?
const user = {
name: "Amit",
getName() {
return this.name;
},
};
console.log(user.getName());
A. "Amit"
B. undefined
C. window.name
D. Error
A.
"Amit"
Call site is user.getName() β this is user.
β‘ Challenge 14 β Guess the output?
"use strict";
const user = {
name: "Amit",
getName() {
return this.name;
},
};
const getName = user.getName;
console.log(getName());
A. "Amit"
B. undefined
C. TypeError
D. ReferenceError
C.
TypeError
Detached method β this === undefined in strict mode β reading .name throws.
Same pitfall as passing obj.method into a callback without binding.
β‘ Challenge 15 β Guess the output?
const user = {
name: "Amit",
getName: () => this.name,
};
console.log(user.getName());
A. "Amit"
B. undefined
C. TypeError
D. Always "global"
B.
undefined (typical module / RN runtime)
Arrows do not take this from the object. They capture outer this.
β‘ Challenge 16 β Guess the output?
const user = { name: "Amit" };
function greet() {
return `Hi ${this.name}`;
}
const boundGreet = greet.bind(user);
console.log(boundGreet());
A. "Hi Amit"
B. "Hi undefined"
C. Error
D. undefined
A.
"Hi Amit"
bind locks this permanently.
π§ͺ Equality, types, and weird JS
β‘ Challenge 17 β Guess the output?
console.log([] == false, [] === false);
A. true true
B. true false
C. false true
D. false false
B.
true false
== coerces both sides toward numbers (0 == 0).
=== also checks type β false.
β‘ Challenge 18 β Guess the output?
console.log(null == undefined, null === undefined);
A. true true
B. true false
C. false true
D. false false
B.
true false
Special == rule: null and undefined are loosely equal.
β‘ Challenge 19 β Guess the output?
console.log(NaN === NaN, Object.is(NaN, NaN));
A. true true
B. true false
C. false true
D. false false
C.
false true
NaN !== NaN with ===.
Object.is treats two NaNs as equal β useful when validating parsed numbers from APIs.
β‘ Challenge 20 β Guess the output?
console.log(typeof null, typeof []);
A. "null" "array"
B. "object" "object"
C. "undefined" "object"
D. "null" "object"
B.
"object" "object"
Historic quirk. Use Array.isArray() for arrays.
π¦ References (React state landmines)
β‘ Challenge 21 β Guess the output?
console.log([] === [], [] == []);
A. true true
B. true false
C. false true
D. false false
D.
false false
Each [] is a new object. References never match.
β‘ Challenge 22 β Guess the output?
const a = [1, 2];
const b = a;
b.push(3);
console.log(a);
A. [1, 2]
B. [1, 2, 3]
C. [3]
D. Error
B.
[1, 2, 3]
a and b point to the same array. Mutating βcopyβ mutates state if you shared the reference.
β‘ Challenge 23 β Guess the output?
const a = { profile: { city: "Delhi" } };
const b = { ...a };
b.profile.city = "Pune";
console.log(a.profile.city);
A. "Delhi"
B. "Pune"
C. undefined
D. Error
B.
"Pune"
Spread is shallow. Nested profile is still shared β classic βI updated state but nested object mutatedβ interview moment.
β‘ Challenge 24 β Guess the output?
const obj = {};
const a = {};
const b = {};
obj[a] = "first";
obj[b] = "second";
console.log(obj[a]);
A. "first"
B. "second"
C. undefined
D. Error
B.
"second"
Object keys become strings β both are "[object Object]". Use Map for object identity keys.
π Arrays & helpers interviewers love
β‘ Challenge 25 β Guess the output?
console.log([1, 30, 4, 21].sort());
A. [1, 4, 21, 30]
B. [1, 21, 30, 4]
C. [30, 21, 4, 1]
D. Error
B.
[1, 21, 30, 4]
Default sort compares as strings. Use (a, b) => a - b for numbers.
β‘ Challenge 26 β Guess the output?
console.log(["1", "2", "3"].map(parseInt));
A. [1, 2, 3]
B. [1, NaN, NaN]
C. [1, 1, 1]
D. Error
B.
[1, NaN, NaN]
map passes (value, index).
parseInt("2", 1) β NaN (bad radix). Prefer .map(Number) or .map((x) => parseInt(x, 10)).
β‘ Challenge 27 β Guess the output?
const items = [1, , 3];
console.log(items.map((x) => x * 2), items.length);
A. [2, 0, 6] 3
B. [2, undefined, 6] 3
C. [2, empty, 6] 3
D. Error
C.
[2, empty, 6] 3
map skips holes and keeps the hole in the result. Length stays 3.
β‘ Challenge 28 β Guess the output?
console.log([0, 1, false, 2, "", 3, null].filter(Boolean));
A. [0, 1, 2, 3]
B. [1, 2, 3]
C. [false, null]
D. Error
B.
[1, 2, 3]
filter(Boolean) drops all falsy values β including legitimate 0 (e.g. quantity / score).
β‘ Challenge 29 β Guess the output?
const values = [1, 2, 3];
const removed = values.splice(1, 1);
console.log(values, removed);
A. [1,2,3] [2]
B. [1,3] [2]
C. [1,3] [1,2]
D. Error
B.
[1,3] [2]
splice mutates and returns removed items. Prefer immutable updates in React state.
β‘ Challenge 30 β Guess the output?
const [a = 1, b = 2] = [undefined, null];
console.log(a, b);
A. 1 2
B. undefined null
C. 1 null
D. null 2
C.
1 null
Defaults apply only for undefined, not null β important when APIs return null.
π§© Parameters, prototypes, classes
β‘ Challenge 31 β Guess the output?
function greet(name = "Guest") {
return name;
}
console.log(greet(), greet(undefined), greet(null));
A. Guest Guest Guest
B. Guest Guest null
C. undefined undefined null
D. Error
B.
Guest Guest null
Defaults fire for missing args / undefined, not for null.
β‘ Challenge 32 β Guess the output?
function total(...nums) {
return nums.reduce((sum, n) => sum + n, 0);
}
console.log(total(1, 2, 3));
A. 6
B. [1,2,3]
C. 123
D. Error
A.
6
Rest params β real array β reduce.
β‘ Challenge 33 β Guess the output?
function login(email, password = "secret", remember) {}
console.log(login.length);
A. 3
B. 2
C. 1
D. 0
C.
1
function.length counts parameters before the first defaulted one.
β‘ Challenge 34 β Guess the output?
const parent = { role: "admin" };
const user = Object.create(parent);
user.name = "Amit";
console.log(user.role, user.hasOwnProperty("role"));
A. "admin" true
B. "admin" false
C. undefined false
D. Error
B.
"admin" false
role comes from the prototype chain, not an own property.
β‘ Challenge 35 β Guess the output?
const parent = { theme: "dark" };
const settings = Object.create(parent);
settings.theme = "light";
delete settings.theme;
console.log(settings.theme);
A. "light"
B. "dark"
C. undefined
D. Error
B.
"dark"
Deleting the own property reveals the inherited one again.
β‘ Challenge 36 β Guess the output?
const config = Object.freeze({
api: { timeout: 1000 },
});
config.api.timeout = 2000;
console.log(config.api.timeout);
A. 1000
B. 2000
C. TypeError
D. undefined
B.
2000
Object.freeze is shallow. Nested objects stay mutable.
β‘ Challenge 37 β Guess the output?
const user = new User();
class User {}
A. A User instance
B. undefined
C. ReferenceError
D. TypeError
C.
ReferenceError
Classes are hoisted into the TDZ β not usable before the declaration line.
β±οΈ Promises, async/await & the event loop
β‘ Challenge 38 β Guess the output?
console.log("start");
Promise.resolve().then(() => console.log("promise"));
console.log("end");
A. start promise end
B. promise start end
C. start end promise
D. Error
C.
start end promise
Sync first β then microtasks.
β‘ Challenge 39 β Guess the output?
console.log("start");
setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("promise"));
console.log("end");
A. start end promise timer
B. start promise end timer
C. start end timer promise
D. timer promise start end
A.
start end promise timer
All microtasks before the next macrotask β even setTimeout(..., 0).
Core RN interview question for loading indicators + API race timing.
β‘ Challenge 40 β Guess the output?
Promise.resolve().then(() => {
console.log(1);
Promise.resolve().then(() => console.log(2));
});
Promise.resolve().then(() => console.log(3));
A. 1 2 3
B. 1 3 2
C. 3 1 2
D. 1 3
B.
1 3 2
First wave queues 1 and 3. Nested 2 is appended after 1 runs.
β‘ Challenge 41 β Guess the output?
async function getValue() {
return 42;
}
console.log(getValue());
A. 42
B. Promise { 42 }
C. undefined
D. Error
B.
Promise { 42 }
Formatting varies by engine, but an async function always returns a Promise.
β‘ Challenge 42 β Guess the output?
async function run() {
console.log("inside");
await 0;
console.log("after await");
}
console.log("before");
run();
console.log("after");
A. before inside after await after
B. before inside after after await
C. before after inside after await
D. Error
B.
before inside after after await
await yields; the continuation runs as a microtask after sync work finishes.
β‘ Challenge 43 β Guess the output?
const slow = new Promise((resolve) => setTimeout(() => resolve("slow"), 10));
const fast = Promise.resolve("fast");
Promise.all([slow, fast]).then(console.log);
A. ["fast", "slow"]
B. ["slow", "fast"]
C. "slowfast"
D. Rejection
B.
["slow", "fast"]
Promise.all keeps input order, not finish order β useful when joining multiple RN API calls.
β‘ Challenge 44 β Guess the output?
Promise.all([
Promise.resolve("ok"),
Promise.reject("failed"),
]).then(console.log).catch(console.log);
A. ["ok", "failed"]
B. "failed"
C. "ok"
D. Nothing
B.
"failed"
One rejection fails the whole Promise.all. Prefer Promise.allSettled when you need every result.
β‘ Challenge 45 β Guess the output?
Promise.resolve("data")
.finally(() => "cleanup")
.then(console.log);
A. "cleanup"
B. "data"
C. undefined
D. Rejection
B.
"data"
A normal return from finally does not replace the resolved value (throwing would).
β‘ Challenge 46 β Guess the output?
async function run() {
[1, 2].forEach(async (n) => {
await Promise.resolve();
console.log(n);
});
console.log("done");
}
run();
A. 1 2 done
B. done 1 2
C. done 2 1
D. Nothing
B.
done 1 2
forEach does not await async callbacks. Use for...of + await for sequential RN work (uploads, chained requests).
πΊοΈ Sets, Maps, modern operators
β‘ Challenge 47 β Guess the output?
const first = { id: 1 };
const second = { id: 1 };
console.log(new Set([first, second]).size);
A. 1
B. 2
C. 0
D. Error
B.
2
Objects compared by reference, not deep equality β same idea as deduping list items incorrectly.
β‘ Challenge 48 β Guess the output?
const map = new Map();
map.set({}, "value");
console.log(map.get({}));
A. "value"
B. undefined
C. null
D. Error
B.
undefined
get({}) uses a new object key, not the stored one.
β‘ Challenge 49 β Guess the output?
const user = {};
console.log(user.profile?.address?.city);
A. null
B. undefined
C. ReferenceError
D. TypeError
B.
undefined
Optional chaining stops safely β great for messy API payloads in RN.
β‘ Challenge 50 β Guess the output?
console.log(0 || 10, 0 ?? 10, "" || "guest", "" ?? "guest");
A. 10 0 "guest" ""
B. 0 0 "" ""
C. 10 10 "guest" "guest"
D. Error
A.
10 0 "guest" ""
|| falls back on any falsy value.
?? falls back only on null / undefined.
Use ?? when 0 or "" are valid (counts, empty search text).
Fast React Native interview revision
| Topic | Remember |
|---|---|
| Hoisting | Declarations hoist; var β undefined; let / const / class β TDZ |
| Closures | Timers/callbacks keep the variables they closed over |
this |
Call site for regular functions; lexical for arrows |
| Equality | Prefer ===; know null == undefined and NaN
|
| References | Arrays/objects share identity; spread is shallow |
| Event loop | Sync β all microtasks β one macrotask |
| Async |
async returns a Promise; forEach wonβt await |
Comment challenge
Drop your score in the comments:
X / 50 β and the one question that still feels unfair π
If this helped your next React Native / JavaScript interview, bookmark it and practice explaining each answer out loud β thatβs what interviewers actually hire for.
Happy coding π
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.