JavaScript Notes
50 tricky JavaScript output questions with detailed answers. Covers type coercion, hoisting, closures, event loop, promises, prototype, scope — ace your 2026 JS interview.
These questions are exactly what interviewers ask to test whether you truly understand JavaScript or just memorized the syntax. Read each one, predict the output, then reveal the answer.
Section 1: Type Coercion & Operators
Q1: Classic Plus Trap
console.log(1 + "2" + 3);
console.log(1 + 2 + "3");
console.log("3" - 1);
console.log("3" * "2");"123" "33" 2 6
Why? + left-to-right: 1+"2" → "12" → "12"+3 → "123". For - and *, strings are coerced to numbers.
Q2: typeof Surprises
console.log(typeof null);
console.log(typeof undefined);
console.log(typeof NaN);
console.log(typeof function(){});
console.log(typeof []);
console.log(typeof {});"object" "undefined" "number" "function" "object" "object"
Note: typeof null === "object" is a famous JavaScript bug kept for backward compatibility. Arrays and plain objects both return "object".
Q3: Loose Equality (==) Coercion
console.log(0 == false);
console.log("" == false);
console.log(null == undefined);
console.log(null == 0);
console.log(NaN == NaN);
console.log([] == false);true true true false false true
Key Rules:
null == undefinedistrue(special rule, no coercion)null == 0isfalse(null only equalsundefined)NaNequals nothing, not even itself[]converts to""then0, which equalsfalse
Q4: Short-Circuit Evaluation
"hello" "world" "JS" "final" "default" 0
?? (Nullish Coalescing): Only falls back for null / undefined — NOT for 0 or "".
Q5: Unary Operators
console.log(+"5");
console.log(+true);
console.log(+false);
console.log(+null);
console.log(+undefined);
console.log(+[]);
console.log(+{});5 1 0 0 NaN 0 NaN
Section 2: Hoisting
Q6: var Hoisting
console.log(a);
var a = 10;
console.log(a);undefined 10
Q7: Function Hoisting Priority
console.log(typeof foo);
var foo = "variable";
function foo() { return "function"; }
console.log(typeof foo);"function" "string"
Why? Function declarations are hoisted before var declarations. After var foo = "variable" executes, it becomes a string.
Q8: Function Expression Not Hoisted
console.log(bar());
var bar = function() { return "bar"; };TypeError: bar is not a function
Why? var bar is hoisted as undefined. Calling undefined() throws a TypeError.
Q9: let TDZ (Temporal Dead Zone)
console.log(x);
let x = 5;ReferenceError: Cannot access 'x' before initialization
Q10: Nested Hoisting
var x = 1;
function outer() {
console.log(x);
var x = 2;
console.log(x);
}
outer();
console.log(x);undefined 2 1
Why? Local var x is hoisted inside outer(), shadowing global x with undefined until assigned.
Section 3: Closures & Scope
Q11: Closure Counter
1 2 1 3
Q12: Loop with var
3 3 3
Q13: Loop with let
0 1 2
Q14: Variable Shadowing
let val = "global";
function test() {
console.log(val); // TDZ!
let val = "local";
console.log(val);
}
test();ReferenceError: Cannot access 'val' before initialization
Why? let val = "local" inside test() creates a TDZ from the start of the block — even though a global val exists, the local one shadows it immediately.
Q15: Lexical Scope
let name = "Global";
function foo() { console.log(name); }
function bar() {
let name = "Bar";
foo();
}
bar();Global
Section 4: this Keyword
Q16: Method vs Function
"JS" undefined
Why? Arrow functions don't have their own this. In the module/strict context, this.name outside any method is undefined.
Q17: Detached Method
const person = {
name: "Rahul",
greet() { console.log("Hi, " + this.name); }
};
const greet = person.greet;
greet(); // 'this' is window/undefined
person.greet(); // 'this' is personHi, undefined Hi, Rahul
Q18: call / apply / bind
Hello, India! Namaste, India. Hey, India?
Section 5: Promises & Async
Q19: Promise Execution Order
1 4 3 2
Rule: Sync → Microtasks (Promises) → Macrotasks (setTimeout).
Q20: Promise Chain
Caught: boom Recovered
Q21: async/await Order
async function fetchData() {
console.log("A");
const result = await Promise.resolve("B");
console.log(result);
console.log("C");
}
console.log("Start");
fetchData();
console.log("End");Start A End B C
Why? await pauses fetchData (schedules continuation as microtask) and returns control to the caller. "End" prints first, then the awaited microtask resumes.
Q22: Promise.all
Failed: Error!
Rule: Promise.all rejects immediately on the first rejection.
Section 6: Arrays & Objects
Q23: Array Equality
console.log([] == []);
console.log([] === []);
console.log({} === {});
const a = [];
const b = a;
console.log(a === b);false false false true
Why? Objects/arrays are reference types. Two separate [] or {} are stored at different memory addresses. Only variables pointing to the same object are equal.
Q24: Object Key Coercion
"ONE" 1
Why? Object keys are always strings. 1 and "1" are the same key — the second assignment overwrites the first.
Q25: Array Spread + Push
[1, 2, 3] [1, 2, 3, 4]
Spread creates a shallow copy — modifying copy doesn't affect arr.
Q26: delete Operator
const obj = { a: 1, b: 2, c: 3 };
delete obj.b;
console.log(obj);
console.log("b" in obj);
console.log(obj.b);{ a: 1, c: 3 }
false
undefinedSection 7: Prototype & Inheritance
Q27: Prototype Chain Lookup
function Animal(name) { this.name = name; }
Animal.prototype.speak = function() {
return this.name + " speaks";
};
const dog = new Animal("Dog");
console.log(dog.speak());
console.log(dog.hasOwnProperty("name"));
console.log(dog.hasOwnProperty("speak"));Dog speaks true false
Q28: instanceof
function Foo() {}
const f = new Foo();
console.log(f instanceof Foo);
console.log(f instanceof Object);
console.log([] instanceof Array);
console.log([] instanceof Object);true true true true
Section 8: Miscellaneous Tricky
Q29: Comma Operator
let x = (1, 2, 3);
console.log(x);3
Comma operator evaluates each expression and returns the last one.
Q30: String Immutability
"hello"
Why? Strings in JavaScript are immutable — character assignment silently fails in non-strict mode.
Q31: Chained Ternary
const n = 15;
const result = n > 10 ? n > 20 ? "big" : "medium" : "small";
console.log(result);"medium"
Q32: Object Destructuring Default
const { a = 10, b = 20 } = { a: 3, b: undefined };
console.log(a, b);3 20
Note: Destructuring defaults apply only when the value is undefined, NOT null.
Q33: Spread vs Rest
15 30
Q34: String Concatenation Trap
console.log([] + []);
console.log([] + {});
console.log({} + []);"" "[object Object]" 0
Why? [] converts to "". {} before + at statement start is parsed as empty block, then +[] becomes 0.
Q35: Number Conversions
console.log(Number(""));
console.log(Number(" "));
console.log(Number(" 123 "));
console.log(Number("123abc"));
console.log(Number(null));
console.log(Number(undefined));
console.log(Number(true));
console.log(Number(false));0 0 123 NaN 0 NaN 1 0
Common Traps Summary 🪤
| Code | Output | Reason |
|---|---|---|
typeof null | "object" | Historic JS bug |
NaN === NaN | false | NaN is not equal to itself |
null == undefined | true | Special JS rule |
null === undefined | false | Strict: different types |
[] == false | true | [] → "" → 0 → false |
0.1 + 0.2 === 0.3 | false | IEEE 754 float precision |
"5" - 3 | 2 | String coerced to number |
"5" + 3 | "53" | Number coerced to string |
Quick Self-Test
Predict these without running:
console.log(0.1 + 0.2 === 0.3); // ?
console.log(typeof typeof 42); // ?
console.log(!!""); // ?
console.log(!!0); // ?
console.log(!!"hello"); // ?
console.log(+""); // ?
console.log(undefined + 1); // ?
console.log(null + 1); // ?false "string" false false true 0 NaN 1
Interview Tips for Tricky Questions 🎯
- Pause and think step-by-step — don't blurt out the first answer.
- Explain type coercion rules aloud as you go.
- State the phase — "during hoisting,
var xgetsundefined..." - Mention edge cases — shows depth (e.g.,
null + 1 = 1, not NaN). - Practice 5 output questions daily — muscle memory for interviews.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for 50 Tricky JavaScript Output Questions 2026 (With Answers).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, interview, tricky, output
Related JavaScript Master Course Topics