✅ 1. What will be the output?
Answer:
false
Explanation:
Each [] creates a new array in memory.
Comparing objects checks reference, not value.
So:
-
left array: memory location A
-
right array: memory location B
A !== B → false
✅ 2. What will this print?
Answer:
Explanation:
Loose equality == does type coercion.
-
[]→""→0 -
"0"→ number0 -
[0]→"0"→0
So all become 0.
✅ 3. What is the output? (Hoisting)
Answer:
undefined
Explanation:
var is hoisted to the top, but only declared, not assigned.
It becomes:
✅ 4. What will be the output?
Answer:
10
Explanation:
Objects are passed by reference, so both a and b point to the same memory.
✅ 5. Output? (Closures)
Answer:
Explanation:
fn remembers count even after outer() finishes.
This is closure.
✅ 6. Tricky: What will happen?
Answer:
❌ ReferenceError
Explanation:
let is hoisted but kept in Temporal Dead Zone (TDZ) until initialization.
Accessing before assignment causes an error.
✅ 7. Promise question
Answer:
Explanation:
JavaScript order:
-
Synchronous
-
Microtask queue (Promises)
-
Macrotask queue (setTimeout)
✅ 8. Output? (this keyword)
Answer:
undefined
Explanation:
When you extract the method into a variable,
this loses context → defaults to undefined (in strict mode).
✅ 9. Output? (Object.freeze)
Answer:
1
Explanation:
freeze() makes object values immutable, so assignment is ignored.
✅ 10. Output? (Set behavior)
Answer:
3
Explanation:
A Set stores unique values only.
🔥 SET 2 — Intermediate to Advanced Logical Questions (With Explanation)
✅ 1. What is the output? (Scope + var)
Answer:
Explanation:
var is function-scoped, not block-scoped.
After the loop, i = 3, so all callbacks print 3.
✅ 2. How to fix the above code to print 0,1,2?
Explanation:
let is block-scoped, so each iteration gets its own i.
✅ 3. Output? (Spread vs Rest)
Answer:
✅ 4. Output? (Destructuring pitfalls)
Answer:
Explanation:
Strings have a length property, so destructuring works.
✅ 5. Output? (Default parameters evaluation)
Answer:
15
Explanation:
b is evaluated after a is available → b = 10.
✅ 6. Output? (Object reference)
Answer:
Explanation:
Two objects never equal unless they reference same memory.
✅ 7. Output? (Prototype chain)
Answer:
true
Explanation:
All objects inherit from Object.prototype.
✅ 8. Output? (Array prototype)
Answer:
true
Explanation:
Array → Array.prototype → Object.prototype.
🔥 9. Tricky Promise + setTimeout
Answer:
Explanation:
-
Sync first
-
Microtask (Promise) next
-
Macrotask (setTimeout) last
🔥 10. Promise + async
Answer:
10
Explanation:
Async function always returns a promise, resolved with the return value.
🔥 11. Output? (this inside arrow function)
Answer:
undefined
Explanation:
Arrow functions don’t bind this.
this refers to global object, not user.
🔥 12. Output? (IIFE + closure)
Answer:
Explanation:
IIFE has its own scope.
🔥 13. Output? (Mutation vs reassignment)
Answer:
5
Explanation:
const prevents reassigning the variable,
not mutating the object.
🔥 14. Output? (Map vs Object key)
Answer:
2
Explanation:
Each {} is a different reference, so Map treats them as unique keys.
🔥 15. Output? (== vs === with null and undefined)
Answer:
Explanation:
null and undefined are only loosely equal to each other.
🔥 SET 3 — Advanced JavaScript Logical Questions (With Deep Explanation)
✅ 1. Output? (Shallow Copy vs Deep Copy)
Answer:
10
Explanation:
Spread operator { ...obj } makes a shallow copy,
so nested objects share the same reference.
✅ 2. Output? (Object keys as string)
Answer:
200
Explanation:
When using objects as keys in normal objects,
they get converted to:
So:
Second assignment overwrites the first.
✅ 3. Output? (setTimeout inside Promise)
Answer:
✅ 4. Output? (async / await + loops)
Answer:
Explanation:
let creates new block-scope variable each iteration.
🔥 5. Very common tricky one:
Answer:
🔥 6. Output? (Implicit return in arrow function)
Answer:
undefined
Explanation:
Because { foo: 1 } looks like a block, not an object.
To return an object implicitly:
🔥 7. Output? (typeof null)
Answer:
Explanation:
A historic JavaScript bug, kept for backward compatibility.
🔥 8. Output? (Function hoisting — tricky)
Answer:
❌ TypeError: foo is not a function
Explanation:
var foo is hoisted, but value assignment is not:
🔥 9. Output? (class hoisting)
Answer:
❌ ReferenceError
Explanation:
Classes are hoisted but kept in Temporal Dead Zone, like let.
🔥 10. Output? (arguments object)
Answer:
Explanation:
Both args and arguments capture all passed values (except in arrow functions).
🔥 11. Output? (Strict mode this)
Answer:
undefined
Explanation:
In strict mode, this inside a normal function is undefined, not global.
🔥 12. Output? (Chained assignments)
Answer:
Explanation (Important):
a.x = a = { n: 2 } is evaluated like:
-
Evaluate left object reference → points to old a
-
Assign
a = { n: 2 } -
Assign
.xon the old object
So:
-
bpoints to old object →b.x = { n: 2 } -
a.xis undefined becauseanow points to new object
Very common in interviews.
🔥 13. Output? (bind)
Answer:
Pranjal
Explanation:
bind() permanently attaches this to the function.
🔥 14. Output? (Chaining Promises)
Answer:
4
🔥 15. Output? (delete operator)
Answer:
{ b: 2 }
Explanation:
delete removes properties from objects.
🔥 SET 4 — VERY HARD & TRICKY JAVASCRIPT QUESTIONS
✅ 1. Output? (Promise inside async)
Answer:
A
Explanation:
If an async function returns a promise, JavaScript does not wrap it again.
It simply returns that promise.
✅ 2. Output? (Double await)
Answer:
10
Explanation:
await on a non-promise returns the value directly.
✅ 3. Output? (Flattening after await)
Answer:
Promise { <resolved>: 5 }
Explanation:
Async functions always return a Promise, never the value directly.
🔥 4. Output? (typeof NaN)
Answer:
number
Explanation:
NaN stands for Not a Number, but its type is "number".
🔥 5. Output? (Boolean conversion)
Answer:
Explanation:
-
Non-empty string is always true
-
0converts to false
🔥 6. Output? (Falsy values)
Answer:
Explanation:
Only 6 values are falsy:
0, "", NaN, null, undefined, false
🔥 7. Output? (Operator precedence)
Answer:
Explanation:
1 < 2 < 3
→ true < 3
→ 1 < 3 → true
3 < 2 < 1
→ false < 1
→ 0 < 1 → true
🔥 8. Output? (Array holes)
Answer:
Explanation:
Missing elements are “holes", not undefined.
Length still counts them.
🔥 9. Output? (forEach skipping holes)
Answer:
Explanation:
forEach skips holes.
🔥 10. Output? (map with hole)
Answer:
[2, <1 empty item>, 6]
Explanation:
map also preserves holes.
🔥 11. Output? (Object comparison)
Answer:
"[object Object][object Object]"
Explanation:
Objects convert to string:
🔥 12. Output? (Adding arrays)
Answer:
"1,23,4"
Explanation:
Arrays convert to strings → "1,2" + "3,4"
🔥 13. Output? (Object keys ordering)
Answer:
["1", "2", "3"]
Explanation:
Object keys are sorted numerically if they look like numbers.
🔥 14. Output? (class private fields)
Answer:
❌ SyntaxError
Explanation:
Private fields are not accessible outside class.
🔥 15. Output? (Chaining then with return undefined)
Answer:
undefined
Explanation:
Missing return, so the first then returns undefined.
🔥 16. Output? (Promise resolve inside setTimeout)
Answer:
Explanation:
-
Timeout callback → macrotask
-
Inside it, the Promise → microtask
After timeout executes, all its microtasks run.
🔥 17. Output? (Prototype override)
Answer:
"blocked"
Explanation:
Prototype methods can be overridden.
🔥 18. Output? (Weird typeof)
Answer:
Explanation:
Classes are special functions.
🔥 19. Output? (instanceof)
Answer:
Explanation:
Array inherits from Object.
🔥 20. Output? (Promise rejection)
Answer:
fixed
Explanation:
catch() returns a resolved promise unless it throws again.
✅ Set 5 — Advanced JavaScript Logical Interview Questions
1️⃣ Output? (Event Loop Tricky)
✅ Answer:
📌 Explanation
-
Synchronous: A, D
-
Microtask queue: Promise → C
-
Macrotask queue: setTimeout → B
2️⃣ Output? (Closures)
❌ Answer:
📌 Explanation
var is function scoped → all functions share same i.
After loop finishes, i = 3.
3️⃣ Fix previous code to print 0,1,2
📌 Because let is block scoped → each iteration creates its own binding.
4️⃣ Output? (Object keys conversion)
❌ Answer: 200
📌 Explanation
Objects used as keys convert to string:
Second assignment overwrites first.
5️⃣ Output? (this binding)
❌ Answer: undefined
📌 Explanation
this depends on the caller.
Here, the caller is global, not user.
Fix:
6️⃣ Output? (Coercion + equality)
❌ Answer:
📌 Explanation
-
1 < 2→ true
true < 3→ 1 < 3 → true -
3 > 2→ true
true > 1→ 1 > 1 → false
7️⃣ Output? (Default params reference)
✅ Answer:
8️⃣ Output? (Set / Map)
Answer: 3
9️⃣ Output? (Promise chain)
Answer:
🔟 Output? (Shallow copy trap)**
❌ Answer: 99
📌 Explanation
Spread operator creates a shallow copy, not deep copy.
✅ Set 6 — Advanced JavaScript Logical + Concept Questions
1️⃣ Output? (async/await + event loop)
✅ Answer:
📌 Explanation
-
"C" → sync
-
"A" → sync inside async
-
awaitmoves next part to microtask queue -
"D" runs before microtask
-
"B" finally runs
2️⃣ Output? (async + setTimeout)
✅ Answer:
📌 Explanation
await pauses this function but not the whole program.
3️⃣ Output? (Generator)
✅ Answer:
4️⃣ Output? (Object.freeze shallow)
✅ Answer:
📌 Explanation
freeze → shallow
nested objects are still mutable.
5️⃣ Output? (setTimeout inside loop)
❌ Answer:
📌 Explanation
var is function scoped → loop ends → i = 4
All callbacks reference the same i.
Fix with let or IIFE.
6️⃣ Output? (prototype chaining)
Answer: 1
📌 Explanation
c → b → a
a contains x, so c.x resolves to 1.
7️⃣ Output? (delete keyword)
Answer:
📌 Explanation
delete removes own properties.
8️⃣ Output? (callback vs promise timing)
Answer:
📌 Explanation
Microtask (promise) → runs before macrotask (setTimeout).
9️⃣ Output? (Deep destructuring)
Answer:
🔟 Output? (Implicit return gotcha)**
❌ Answer: [undefined, undefined, undefined]
📌 Explanation
Using {} requires explicit return.
Fix: