Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4073,6 +4073,217 @@ setTimeout(() => {
},2000)
```

# 🥷 Ninja-Level JavaScript Questions

A collection of tricky, edge-case, and uncommon JavaScript questions for advanced learners and interview prep. These questions are designed to test deep understanding and “gotchas” in JavaScript.

---

## Questions

1. Why does this print `undefined`?
```js
console.log(a);
var a = 10;


Answer:
var declarations are hoisted to the top of their scope. The declaration (var a) is hoisted, but the assignment (a = 10) is not. So at the time of console.log(a), a exists but is undefined.
-------------------------------------------
2.Why does [] == ![] evaluate to true?
Answer:

![] converts the array to boolean → true, then negates → false.

[] == false triggers type coercion: array → primitive string → "", then "" == false → true.

-------------------------------------------
3.Difference between NaN === NaN and Object.is(NaN, NaN).
Answer:

NaN === NaN → false because NaN is never equal to itself.

Object.is(NaN, NaN) → true because Object.is treats NaN as equal to itself.
-------------------------------------------
4.What will this print and why?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1000);
}
var is function-scoped, so all timeouts share the same i, which is 3 after the loop.
Using let i would print 0,1,2 because let is block-scoped.
-------------------------------------------
5.Explain why typeof null returns "object".
Answer:
A historical bug in JavaScript. null is a primitive, but typeof null incorrectly returns "object" for legacy reasons.
-------------------------------------------
6.What will this print?
console.log([] + {});
console.log({} + []);
Answer:

[] + {} → "" + "[object Object]" → "[object Object]"

{} can be interpreted as a block; adding + [] triggers coercion → context matters
-------------------------------------------
7.Explain sparse arrays:
let a = [1,2,3];
a[10] = 99;
console.log(a.length);
Answer:
a.length → 11. Arrays in JS are sparse, meaning indices 3–9 are empty but counted in .length.
-------------------------------------------
8.Why does 0.1 + 0.2 === 0.3 return false?
Answer:
Floating-point precision error. 0.1 + 0.2 = 0.30000000000000004.
-------------------------------------------
9.Difference between == and === when comparing null and undefined.
Answer:

null == undefined → true (type coercion)

null === undefined → false (strict equality, no coercion)
-------------------------------------------
10.What happens here?
let a = [1,2,3];
a[5] = undefined;
console.log(a.length); // 6
console.log(a); // [1,2,3,undefined × 2, undefined]

Answer:
Assigning to index 5 creates sparse elements (index 3 and 4 are empty), .length counts all positions.
-------------------------------------------
11.Why does this work?
(() => {}) instanceof Function; // true

Answer:
Arrow functions are special kinds of functions, so they are instances of Function.
-------------------------------------------
12.What happens if you do:
delete Array.prototype.push;

Answer:
Removes the push method from all arrays. After this, [].push() will throw an error.
-------------------------------------------
13.Difference between call, apply, and bind

Answer:

call(thisArg, arg1, arg2, ...) → invokes function immediately.

apply(thisArg, [args]) → like call but takes args as array.

bind(thisArg, args) → returns a new function with bound this.
-------------------------------------------
14.What will this print?
console.log([] == false); // true
console.log([] === false); // false

Answer:
== triggers coercion: [] → "", then "" == false → true. === checks type → array !== boolean.
-------------------------------------------
15.Explain this behavior in arrow functions vs regular functions

Answer:

Regular function: this depends on how function is called.

Arrow function: this is lexically inherited from enclosing scope.
-------------------------------------------
16.How do var, let, const behave differently in loops with async callbacks?

Answer:

var → function-scoped → same variable for all iterations → closure issues.

let/const → block-scoped → each iteration has its own variable → works as expected.
-------------------------------------------
17.What is the output and why?
console.log('5' - 3); // 2
console.log('5' + 3); // "53"

Answer:

- triggers numeric coercion → '5' → 5

+ triggers string concatenation → '5' + '3' = "53"

-------------------------------------------
18.. Explain event loop order with setTimeout vs Promise.resolve().then()

Answer:

Promises are microtasks, executed before the next macrotask (setTimeout).

Example:setTimeout(() => console.log('timeout'), 0);
Promise.resolve().then(() => console.log('promise'));
// Output: promise, timeout

-------------------------------------------
19.What will this print?
console.log(typeof NaN); // "number"
console.log(typeof Infinity); // "number"

Answer:
Both NaN and Infinity are number primitives.
-------------------------------------------
20.. Explain prototypal inheritance and difference between Object.create() and new

Answer:

Object.create(proto) → creates object with specified prototype, no constructor call.

new Constructor() → creates object, sets prototype, runs constructor.

-------------------------------------------

21.Why does this happen?
console.log(true + false); // 1

Answer:
Boolean values are converted to numbers: true → 1, false → 0
-------------------------------------------
22.What will this print?
console.log([] + []); // ""
console.log([] + {}); // "[object Object]"
Answer:

[] + [] → empty string

[] + {} → "" + "[object Object]" → "[object Object]"
-------------------------------------------
23.Explain short-circuit evaluation in:
true || false && false; // true

Answer:

&& has higher precedence → false && false → false

true || false → true
-------------------------------------------
24.What is the output and why?
console.log(1 < 2 < 3); // true
console.log(3 > 2 > 1); // false
Answer:

1 < 2 → true → true < 3 → true coerced to 1 → 1 < 3 → true

3 > 2 → true → true > 1 → 1 > 1 → false
-------------------------------------------
25.Difference between primitive and reference types in JS assignments

Answer:

Primitive (number, string, boolean, null, undefined, symbol) → copied by value.

Reference (objects, arrays, functions) → copied by reference; changes affect original.








<details><summary><b>Answer</b></summary>
The above program will print 0 to 9 sequentially.
</details>
Expand Down