JavaScript interview topics from beginning to end
Chapter 1: JavaScript Variables
1. Variables in JavaScript
JavaScript has three ways to declare variables:
Var (old way — avoid using)
Function-scoped
Gets hoisted (initialized with undefined)
Allows re-declaration
Allows re-assignment
var x = 10;
var x = 20; // allowed
let (modern, block-scoped)
Block-scoped {}
No re-declaration
Allows re-assignment
Hoisted but NOT initialized → Temporal Dead Zone (TDZ)
let a = 10;
a = 20; // allowed
const (block-scoped, cannot reassign)
Block-scoped
Cannot reassign
Must be initialized at declaration
const PI = 3.14;
// PI = 4; ❌ not allowed
Interview Trick
const does NOT make objects immutable:
const user = { name: "John" }
[Link] = "Sam"; // ✔ allowed
2. Data Types in JavaScript
JavaScript has 7 primitive types + 1 non-primitive.
Primitive means “copied by value”.
1. string
2. number
3. boolean
4. null → intentional empty value
5. undefined → declared but not assigned
6. symbol
7. bigint
let name = "Alice"; // string
let age = 25; // number
let isReady = true; // boolean
let x; // undefined
let y = null; // null
let id = Symbol("id"); // symbol
let big = 123n; // bigint
3. Non-Primitive (Reference) Types
Stored in heap
Copied by reference
Example:
objects
arrays
functions
let obj = { name: "Bob" };
let arr = [1, 2, 3];
function greet() {}
4. Dynamic & Weak Typing
JavaScript is dynamically typed:
JavaScript being “dynamically typed” means that variable types are determined at
runtime based on the value currently stored, not fixed in advance in the code.
In a dynamically typed language like JavaScript, you do not declare types for variables.
let value = 10;
value = "hello"; // allowed
5. Type Coercion
JavaScript automatically converts types in some situations.
Implicit Coercion
'2' + 2 // "22"
'2' - 2 // 0
true + 1 // 2
Explicit Coercion
Number("5") // 5
String(100) // "100"
Boolean(1) // true
Implicit coercion happens automatically when JavaScript converts types during operations like
+, -, ==.
Explicit coercion is when a developer manually converts types using functions like Number(),
String(), or operators like + and !!.
6. Arrays and Objects
Object
An object is a collection of key–value pairs.
Keys are always strings (or symbols), values can be anything.
const obj = {
name: "John",
age: 30
};
Object Storage
Objects are stored in the heap (reference type).
Variables store a reference (pointer) to the heap location.
const a = { x: 1 };
const b = a;
b.x = 2;
[Link](a.x); // 2 ❗
Property Access
Dot notation([Link])
Bracket notation(obj["name"])
Important Object Methods (interview)
[Link](obj) → array of keys
[Link](obj) → array of values
[Link](obj) → array of [key, value] pairs
[Link]() → reverse of entries
Mutation vs Non-Mutation array methods
Mutates original array (destructive):
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
Does NOT mutate (pure):
map()
filter()
reduce()
slice()
concat()
TOP JAVASCRIPT INTERVIEW QUESTIONS — ARRAYS & OBJECTS
1. What is the difference between == and === when comparing objects?
const a = { x: 1 };
const b = { x: 1 };
[Link](a == b);//false
[Link](a === b);//false
Even though a and b have the same content, they are different objects in memory.
a points to one memory location
b points to another memory location
In JavaScript, objects are compared by reference, not by value.
Hence
{} === {} // false
[] === [] // false
2. How do you check if a value is an array in JavaScript?
[Link]()
[Link]([1, 2, 3]); // true
[Link]("hello"); // false
[Link]({});//false
instanceof Array
[1, 2, 3] instanceof Array; // true
"hello" instanceof Array; // false
typeof
typeof [1, 2, 3]; // "object"
Arrays are a special kind of object, so typeof cannot distinguish them.
[Link]()
[Link]([1, 2, 3]); // "[object Array]"
[Link]({}); // "[object Object]"
Good because:
Works across iframes
Older approach (before ES5)
Use case:
Legacy code
Framework internals
3. What is the difference between shallow copy and deep copy?
1. Shallow Copy (copies only 1 level)
A shallow copy creates a new object, but nested objects are still shared (copied by
reference).
It copies only the top-level properties.
const obj1 = {
a: 1,
b: { x: 10 }
};
const obj2 = { ...obj1 }; // Shallow copy
obj2.b.x = 999;
[Link](obj1.b.x); // 999 ❗
Shallow Copy Methods
Spread operator: { ...obj }, [ ...arr ]
[Link]({}, obj)
[Link]()
[Link]()
const obj1 = {
a: 1,
b: { x: 10 }
};
const obj2 = structuredClone(obj1); // Deep copy
obj2.b.x = 999;
[Link](obj1.b.x); // 10 ✔ (unchanged)
Deep Copy Methods
structuredClone(obj) (best modern method)
[Link]([Link](obj)) (common but has limitations)
Libraries like:
o Lodash _.cloneDeep()
[Link]([Link](obj)) performs a deep copy only for simple JSON-safe data.
It fails for functions, dates, symbols, undefined, Maps, Sets, classes, circular references, and
many other non-JSON values.
That’s why structuredClone() or _.cloneDeep() is preferred for true deep cloning.