Can You Guess These 50 JavaScript Outputs? (React Native Interview Edition)

작성자

카테고리:

← 피드로
DEV Community · Amit Kumar · 2026-07-25 개발(SW)

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)

  1. Read the snippet.
  2. Pick A / B / C / D before you scroll.
  3. Open 👀 Reveal answer only after you commit.
  4. 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);

Enter fullscreen mode Exit fullscreen mode

A. 119

B. 1234

C. 1199

D. NaN

👀 Reveal answer
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;

Enter fullscreen mode Exit fullscreen mode

A. 10

B. undefined

C. ReferenceError

D. null

👀 Reveal answer
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;

Enter fullscreen mode Exit fullscreen mode

A. 10

B. undefined

C. ReferenceError

D. null

👀 Reveal answer
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;
}

Enter fullscreen mode Exit fullscreen mode

A. 5

B. undefined

C. TypeError

D. ReferenceError

👀 Reveal answer
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;
};

Enter fullscreen mode Exit fullscreen mode

A. 5

B. undefined

C. TypeError

D. ReferenceError

👀 Reveal answer
C. TypeError

add is hoisted as undefined, then you call undefined(...).


⚡ Challenge 6 — Guess the output?

console.log(name);
const name = "Amit";

Enter fullscreen mode Exit fullscreen mode

A. "Amit"

B. undefined

C. ReferenceError

D. TypeError

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. 1 "undefined"

B. 2 "undefined"

C. 2 "number"

D. Error

👀 Reveal answer
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);
}

Enter fullscreen mode Exit fullscreen mode

A. 0 1 2

B. 3 3 3

C. 0 0 0

D. Error

👀 Reveal answer
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);
}

Enter fullscreen mode Exit fullscreen mode

A. 0 1 2

B. 3 3 3

C. 0 0 0

D. Error

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. 1 1

B. 0 1

C. 1 2

D. Error

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. 1 2 1

B. 1 2 3

C. 1 1 1

D. 0 1 0

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. 5, 6, 6

B. 5, 7, 7

C. 6, 6, 7

D. 6, 7, 7

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. "Amit"

B. undefined

C. window.name

D. Error

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. "Amit"

B. undefined

C. TypeError

D. ReferenceError

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. "Amit"

B. undefined

C. TypeError

D. Always "global"

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. "Hi Amit"

B. "Hi undefined"

C. Error

D. undefined

👀 Reveal answer
A. "Hi Amit"

bind locks this permanently.


🧪 Equality, types, and weird JS

⚡ Challenge 17 — Guess the output?

console.log([] == false, [] === false);

Enter fullscreen mode Exit fullscreen mode

A. true true

B. true false

C. false true

D. false false

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. true true

B. true false

C. false true

D. false false

👀 Reveal answer
B. true false

Special == rule: null and undefined are loosely equal.


⚡ Challenge 19 — Guess the output?

console.log(NaN === NaN, Object.is(NaN, NaN));

Enter fullscreen mode Exit fullscreen mode

A. true true

B. true false

C. false true

D. false false

👀 Reveal answer
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 []);

Enter fullscreen mode Exit fullscreen mode

A. "null" "array"

B. "object" "object"

C. "undefined" "object"

D. "null" "object"

👀 Reveal answer
B. "object" "object"

Historic quirk. Use Array.isArray() for arrays.


📦 References (React state landmines)

⚡ Challenge 21 — Guess the output?

console.log([] === [], [] == []);

Enter fullscreen mode Exit fullscreen mode

A. true true

B. true false

C. false true

D. false false

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. [1, 2]

B. [1, 2, 3]

C. [3]

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. "Delhi"

B. "Pune"

C. undefined

D. Error

👀 Reveal answer
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]);

Enter fullscreen mode Exit fullscreen mode

A. "first"

B. "second"

C. undefined

D. Error

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. [1, 4, 21, 30]

B. [1, 21, 30, 4]

C. [30, 21, 4, 1]

D. Error

👀 Reveal answer
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));

Enter fullscreen mode Exit fullscreen mode

A. [1, 2, 3]

B. [1, NaN, NaN]

C. [1, 1, 1]

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. [2, 0, 6] 3

B. [2, undefined, 6] 3

C. [2, empty, 6] 3

D. Error

👀 Reveal answer
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));

Enter fullscreen mode Exit fullscreen mode

A. [0, 1, 2, 3]

B. [1, 2, 3]

C. [false, null]

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. [1,2,3] [2]

B. [1,3] [2]

C. [1,3] [1,2]

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. 1 2

B. undefined null

C. 1 null

D. null 2

👀 Reveal answer
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));

Enter fullscreen mode Exit fullscreen mode

A. Guest Guest Guest

B. Guest Guest null

C. undefined undefined null

D. Error

👀 Reveal answer
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));

Enter fullscreen mode Exit fullscreen mode

A. 6

B. [1,2,3]

C. 123

D. Error

👀 Reveal answer
A. 6

Rest params → real array → reduce.


⚡ Challenge 33 — Guess the output?

function login(email, password = "secret", remember) {}
console.log(login.length);

Enter fullscreen mode Exit fullscreen mode

👀 Reveal answer
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"));

Enter fullscreen mode Exit fullscreen mode

A. "admin" true

B. "admin" false

C. undefined false

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. "light"

B. "dark"

C. undefined

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. 1000

B. 2000

C. TypeError

D. undefined

👀 Reveal answer
B. 2000

Object.freeze is shallow. Nested objects stay mutable.


⚡ Challenge 37 — Guess the output?

const user = new User();
class User {}

Enter fullscreen mode Exit fullscreen mode

A. A User instance

B. undefined

C. ReferenceError

D. TypeError

👀 Reveal answer
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");

Enter fullscreen mode Exit fullscreen mode

A. start promise end

B. promise start end

C. start end promise

D. Error

👀 Reveal answer
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");

Enter fullscreen mode Exit fullscreen mode

A. start end promise timer

B. start promise end timer

C. start end timer promise

D. timer promise start end

👀 Reveal answer
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));

Enter fullscreen mode Exit fullscreen mode

A. 1 2 3

B. 1 3 2

C. 3 1 2

D. 1 3

👀 Reveal answer
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());

Enter fullscreen mode Exit fullscreen mode

A. 42

B. Promise { 42 }

C. undefined

D. Error

👀 Reveal answer
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");

Enter fullscreen mode Exit fullscreen mode

A. before inside after await after

B. before inside after after await

C. before after inside after await

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. ["fast", "slow"]

B. ["slow", "fast"]

C. "slowfast"

D. Rejection

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. ["ok", "failed"]

B. "failed"

C. "ok"

D. Nothing

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. "cleanup"

B. "data"

C. undefined

D. Rejection

👀 Reveal answer
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();

Enter fullscreen mode Exit fullscreen mode

A. 1 2 done

B. done 1 2

C. done 2 1

D. Nothing

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

👀 Reveal answer
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({}));

Enter fullscreen mode Exit fullscreen mode

A. "value"

B. undefined

C. null

D. Error

👀 Reveal answer
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);

Enter fullscreen mode Exit fullscreen mode

A. null

B. undefined

C. ReferenceError

D. TypeError

👀 Reveal answer
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");

Enter fullscreen mode Exit fullscreen mode

A. 10 0 "guest" ""

B. 0 0 "" ""

C. 10 10 "guest" "guest"

D. Error

👀 Reveal answer
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; varundefined; 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 🚀

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다