JavaScript Interview Preparation — Phase 1: Fundamentals
Detailed Notes, Concepts & Interview Examples
1■■ Variables — var, let, const
• var is function-scoped; let and const are block-scoped.
• var can be re-declared; let/const cannot.
• Hoisting: var is hoisted with undefined; let/const are hoisted but remain in TDZ.
• const must be initialized immediately.
function test() {
if (true) {
var x = 10;
let y = 20;
const z = 30;
[Link](x, y, z);
}
[Link](x); // Works
// [Link](y); // Error
}
2■■ Data Types
• Primitive types: string, number, boolean, undefined, null, bigint, symbol.
• Reference types: object, array, function.
• typeof null === "object" (legacy bug).
• Arrays are objects; check with [Link]().
3■■ Type Coercion & Conversion
• '+' operator prefers string concatenation.
• '-', '*', '/' trigger numeric coercion.
• Boolean conversion: 0, '', null, undefined, NaN → false.
• NaN is of type 'number'.
[Link](1 + "2"); // "12"
[Link]("5" * 2); // 10
[Link]("A" - 1); // NaN
4■■ Comparison: == vs ===
• '==' performs type coercion before comparison.
• '===' checks both value and type (no coercion).
• Objects compared by reference, not value.
[Link](0 == false); // true
[Link](0 === false); // false
[Link]({} === {}); // false
5■■ Conditionals & Loops
• Falsy values: false, 0, '', null, undefined, NaN.
• for...in iterates keys; for...of iterates values.
• break exits loop; continue skips current iteration.
for (let v of [10, 20]) [Link](v); // 10, 20
for (let i in [10, 20]) [Link](i); // 0, 1
6■■ Functions
• Function Declarations are hoisted.
• Function Expressions are not hoisted.
• Arrow functions don’t have their own 'this'.
• Default and Rest parameters simplify argument handling.
function greet() { [Link]("Hi"); }
const sayHi = () => [Link]("Hello!");
function sum(...nums) { return [Link]((a, b) => a + b); }
■ Summary of Phase 1 Takeaways
• var → function-scoped; let/const → block-scoped.
• Hoisting: var undefined; let/const in TDZ.
• Use '===' for strict equality.
• Arrow functions inherit lexical 'this'.
• Objects are compared by reference.
• Always initialize const immediately.