JavaScript Interview Questions (30 MCQs)
30 Essential JavaScript Interview Questions
1. null vs undefined
| Feature | null | undefined |
|---|---|---|
| Meaning | Intentional absence of value | Variable declared but not assigned |
typeof | "object" (bug) | "undefined" |
== | null == undefined → true | Same |
=== | null === undefined → false | Different types |
| JSON | Serializable (null) | Omitted from JSON |
2. What is a Closure?
A closure is a function that retains access to its lexical scope even when executed outside that scope.
function createCounter() {
let count = 0;
return {
increment: () => ++count,
getCount: () => count,
};
}
const counter = createCounter();
counter.increment(); // 1
counter.getCount(); // 13. Temporal Dead Zone
The period between entering a scope and the point where a let/const variable is declared. Accessing it throws ReferenceError.
4. call() vs apply() vs bind()
| Method | Invokes? | Args Format |
|---|---|---|
call(ctx, a, b) | Yes, immediately | Comma-separated |
apply(ctx, [a, b]) | Yes, immediately | Array |
bind(ctx, a, b) | No, returns new fn | Comma-separated |
5. Prototypal Inheritance
Objects delegate to other objects via the prototype chain. When a property isn't found on an object, JS looks up its __proto__ chain until it finds it or reaches null.
6. Event Loop
See the dedicated lesson. Key: Sync → Microtasks (all) → 1 Macrotask → Microtasks (all) → Render → Repeat.
7. Shallow vs Deep Copy
| Method | Type | Handles Nested? |
|---|---|---|
Object.assign() | Shallow | ❌ |
{...obj} | Shallow | ❌ |
JSON.parse(JSON.stringify()) | Deep | ❌ (loses functions, dates, undefined) |
structuredClone() | Deep | ✅ (modern, handles most types) |
8. Map vs Object
| Feature | Map | Object |
|---|---|---|
| Key types | Any (objects, functions) | String/Symbol only |
| Order | Insertion order guaranteed | Mostly insertion order |
| Size | map.size | Object.keys(obj).length |
| Performance | Better for frequent add/delete | Better for static lookups |
| Iteration | Directly iterable | Need Object.entries() |
9. IIFEs
(function() {
// Private scope — variables don't leak
const secret = 42;
})();
// Modern alternative: ES Modules provide file-level scope10. How async/await works under the hood
Async functions are transformed into state machines using generators. Each await is a yield point that suspends execution and resumes when the Promise resolves.
11. Event Bubbling vs Capturing
Bubbling: Event goes from target UP to root (default). Capturing: Event goes from root DOWN to target. Use addEventListener("click", fn, true) for capture phase.
12. __proto__ vs prototype
__proto__ is on every object (its parent reference). prototype is only on functions (template for instances).
13. Strict Mode
"use strict";
// Makes JS safer:
// - No undeclared variables
// - No duplicate params
// - No `with` statement
// - `this` is undefined in plain functions (not window)
// - Octal literals throw errors14–15. Higher-Order Functions & Object.freeze vs Object.seal
| Method | Add Props | Delete Props | Modify Values |
|---|---|---|---|
Object.freeze() | ❌ | ❌ | ❌ (shallow) |
Object.seal() | ❌ | ❌ | ✅ |
Object.preventExtensions() | ❌ | ✅ | ✅ |
16. innerHTML vs textContent
| Property | Parses HTML? | XSS Risk? | Performance |
|---|---|---|---|
innerHTML | Yes | ⚠️ Yes | Slower (re-parses DOM) |
textContent | No (plain text) | ✅ Safe | Faster |
17–20. Advanced Topics
Deadlocks in JS? Not traditional deadlocks (single-threaded), but microtask starvation can mimic it.
Promise.all vs allSettled: all rejects on first failure; allSettled waits for all and reports each result.
Currying: f(a, b, c) → f(a)(b)(c). Useful for creating specialized functions from general ones.
Memory Leak Detection: Chrome DevTools → Memory → Heap Snapshots → Compare to find growing objects.
21–25. Expert Level
Reflect API: Provides default operations for Proxy traps. Use Reflect.get() instead of target[prop] in proxy handlers.
for...in vs for...of:
| Loop | Iterates Over | Best For |
|---|---|---|
for...in | Keys (+ inherited) | Objects |
for...of | Values (iterables) | Arrays, Maps, Sets, Strings |
26–30. Performance & Patterns
Debounce: Wait until action stops → then execute (search input). Throttle: Execute at most once per interval (scroll events).
WeakMap vs Map: WeakMap keys must be objects and don't prevent GC. Perfect for associating data with DOM elements.
Memoization: Cache function results. Use for pure functions with expensive computation.
Pure Functions: Same input → same output, no side effects. Foundation of functional programming and testable code.