📘 JavaScript Interview Cheat-Sheet
1️⃣ JavaScript Basics
- Single-threaded, non-blocking
- Uses Event Loop
- Interpreted, JIT compiled
- Dynamically typed
2️⃣ Variables
var a = 10; // function scoped ❌
let b = 20; // block scoped ✅
const c = 30; // block scoped, no reassignment ✅
Rule: Prefer const → then let → avoid var
5️⃣ Hoisting
console.log(a); // undefined
var a = 10;
var→ hoisted + initializedlet / const→ hoisted ❌ but in TDZ
Great follow-up 👍
This is a very important concept, and interviewers often expect this exact explanation.
✅ Simple Meaning
Initialization means assigning a value to a variable in memory.
In short:
- Declare → create the variable name
- Initialize → give it a value
🔁 Declaration vs Initialization
Example
let x; // declaration (no value yet)
x = 10; // initialization (value assigned)
🧠 Now relate this to hoisting
var Example
console.log(a); // undefined
var a = 5;
What JavaScript actually does:
var a; // declaration + initialization with undefined
console.log(a);
a = 5; // real value assigned later
✅ Explanation
varis hoisted and initialized withundefined,
so accessing it before assignment does not throw an error.
❌ let and const Example
console.log(b); // ReferenceError
let b = 5;
What happens internally:
// b is declared (hoisted)
// ❌ NOT initialized
console.log(b); // TDZ error
b = 5; // initialization happens here
✅ Explanation
letandconstare hoisted but not initialized,
so they cannot be accessed before the declaration line.
⏳ Temporal Dead Zone (TDZ) – Linked to Initialization
TDZ exists because the variable is not initialized yet.
{
// TDZ
let x = 10; // initialization happens here
}
6️⃣ Scope
- Global
- Function
- Block (
let,const)
{
let x = 10;
}
// x ❌ not accessible
7️⃣ Functions
Normal
function sum(a, b) {
return a + b;
}
Arrow
const sum = (a, b) => a + b;
⚠ Arrow functions:
- No
this - No
arguments - Cannot be constructors
8️⃣ this Keyword
Depends on how function is called
obj.method() // this = obj
fn() // this = window / undefined (strict)
Arrow → inherits this from parent
9️⃣ Closures ⭐ (VERY IMPORTANT)
function outer() {
let count = 0;
return function inner() {
return ++count;
};
}
👉 Function remembers outer scope variables
🔟 Arrays (High Frequency)
arr.map() // transform
arr.filter() // condition
arr.reduce() // accumulate
arr.reduce((acc, cur) => acc + cur, 0);
1️⃣1️⃣ Objects
const user = {
name: "Alex",
age: 25
};
Access:
user.name
user["age"]
1️⃣2️⃣ Spread vs Rest
Spread
const a = [1,2];
const b = [...a,3];
Rest
function sum(...nums) {
return nums.reduce((a,b) => a+b);
}
1️⃣3️⃣ Destructuring
const { name, age } = user;
const [a, b] = [1, 2];
1️⃣4️⃣ Shallow vs Deep Copy
const b = { ...a }; // shallow
Deep:
structuredClone(obj);
1️⃣5️⃣ Asynchronous JavaScript
Callback
setTimeout(() => {}, 1000);
Promise
fetch(url).then(res => res.json());
Async / Await
const data = await fetch(url);
1️⃣6️⃣ Promise APIs
Promise.all() // fails fast
Promise.allSettled()
Promise.race()
Promise.any()
1️⃣7️⃣ Event Loop ⭐⭐⭐
Order:
- Call Stack
- Microtask Queue (
Promise) - Macrotask Queue (
setTimeout)
Promise.resolve().then(()=>console.log("micro"));
setTimeout(()=>console.log("macro"),0);
Output:
micro
macro
1️⃣8️⃣ Error Handling
try {
risky()
} catch (e) {
console.log(e.message);
}
2️⃣0️⃣ Strict Mode
"use strict";
- Prevents silent errors
- Safer code
this=undefinedin functions
🔥 Rapid-Fire Interview One-Liners
1️⃣ Rule #1: JavaScript Execution Order
Always explain in this order 👇
1. Synchronous code 2. Microtasks (Promises) 3. Macrotasks (setTimeout, setInterval)
2️⃣ Example: Event Loop (MOST COMMON)
Code
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Output
A
D
C
B
✅ How to Explain (Interview English)
JavaScript first executes all synchronous code, so
"A"and"D"are printed. Then it checks the microtask queue, where Promises run, so"C"is printed. Finally, it executes macrotasks likesetTimeout, so"B"is printed last.
3️⃣ var in Loop Example
Code
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
Output
3
3
3
✅ How to Explain
varis function-scoped, not block-scoped. By the timesetTimeoutexecutes, the loop has already finished andiequals 3. All callbacks reference the same variable.
4️⃣ Fix with let
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
Output
0
1
2
✅ Explanation
letcreates a new block-scoped variable for each iteration, so each callback gets its own value ofi.
5️⃣ Object Comparison
Code
{} === {}
Output
false
✅ Explanation
Objects are compared by reference, not by value. Even though both objects look identical, they are stored at different memory locations.
6️⃣ Arrow Function this
Code
const obj = {
name: "JS",
show: () => {
console.log(this.name);
}
};
obj.show();
Output
undefined
✅ Explanation
Arrow functions do not have their own
this. They inheritthisfrom their surrounding scope, which in this case is the global scope, not the object.
7️⃣ Closure Example
Code
function counter() {
let count = 0;
return () => count++;
}
const c = counter();
console.log(c());
console.log(c());
Output
0
1
✅ Explanation
The inner function forms a closure and remembers the
countvariable even after the outer function has finished executing.
8️⃣ typeof null
typeof null;
Output
"object"
✅ Explanation
This is a known JavaScript bug from the early days of the language and was never fixed for backward compatibility.
9️⃣ parseInt Trap
Code
["1","2","3"].map(parseInt);
Output
[1, NaN, NaN]
✅ Explanation
mappasses two arguments: value and index.parseInttreats the second argument as radix, soparseInt("2",1)andparseInt("3",2)returnNaN.