Loading content…
Loading content…
A comprehensive compile of junior, mid, and senior level JavaScript interview questions and answers covering closures, event loop priorities, debounce/throttle algorithms, and prototype mechanics
var, let, and const differ?var declarations are hoisted and initialized with undefined. Calling them before declaration returns undefined.let and const declarations are hoisted but are not initialized. They reside in the Temporal Dead Zone (TDZ) from the start of the block until the line of declaration is reached. Accessing them early throws a ReferenceError.// Hoisting comparison:
console.log(x); // returns undefined (hoisted var)
var x = 5;
console.log(y); // Throws ReferenceError (TDZ let)
let y = 10;
== and triple equals ===?== (Loose Equality) compares two values for equality after performing type coercion (converting the values to a common type first).=== (Strict Equality) compares both the value and the type. No type coercion occurs; if the types are different, it returns false.console.log(5 == "5"); // true (string coerced to number)
console.log(5 === "5"); // false (number !== string)
console.log(null == undefined); // true
console.log(null === undefined); // false
function multiplier(factor) {
return function (num) {
return num * factor; // "factor" is captured by closure!
};
}
const double = multiplier(2);
console.log(double(5)); // 10
queueMicrotask).setTimeout, DOM events, network fetches).call, apply, and bind differ?this binding) of a function:
call: Invokes the function immediately, passing arguments individually (comma-separated).apply: Invokes the function immediately, passing arguments as an array.bind: Returns a new function with this bound, allowing arguments to be pre-filled. It does not invoke the function immediately.const user = { name: "Alice" };
function greet(greeting, punctuation) {
console.log(`${greeting}, ${this.name}${punctuation}`);
}
greet.call(user, "Hello", "!"); // "Hello, Alice!"
greet.apply(user, ["Hi", "."]); // "Hi, Alice."
const bound = greet.bind(user);
bound("Welcome", "!"); // "Welcome, Alice!"
debounce and throttle. Explain their differences and production use cases.// Debounce Implementation
function debounce(fn, delay) {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
// Throttle Implementation
function throttle(fn, limit) {
let inThrottle = false;
return function (...args) {
if (!inThrottle) {
fn.apply(this, args);
inThrottle = true;
setTimeout(() => {
inThrottle = false;
}, limit);
}
};
}
class keyword?class Dog extends Animal), JS creates function constructors..prototype object, rather than copied to every instance.__proto__ to locate the method on the class prototype.JavaScript Interview Preparation Checklist
let/const inside the TDZ.bind/call/apply override the execution context this.Marking it complete updates your roadmap progress percentage.