REVIEWED STARTER COLLECTIONJavaScript Interview Questions
Short answer se start karo, phir interview-level explanation, small code example aur common follow-up revise karo. Ye collection canonical JavaScript tutorial ko replace nahi karta.
1. What is the difference between let, const and var?
Short Answerlet and const are block-scoped; var is function-scoped. const prevents reassignment of the binding.
Detailed Explanationlet/const use the temporal dead zone before declaration. var declarations are hoisted and initialized with undefined. A const object can still have mutable properties.
const user = { name: "Aman" };
user.name = "Riya"; // allowed
// user = {}; // reassignment is not allowed
Common follow-up: Does const make an object immutable? No; it protects the variable binding, not every nested value.
Learn this concept →
2. What is a JavaScript closure?
Short AnswerA closure is a function together with access to variables from the lexical environment where the function was created.
Detailed ExplanationAn inner function can keep using surrounding variables after its outer function has returned. This makes closures useful for private state, callbacks and function factories.
function makeCounter() {
let count = 0;
return () => ++count;
}
const counter = makeCounter();
Common follow-up: Closures can retain referenced state, so unnecessary long-lived references may also retain memory.
Learn this concept →
3. What is the difference between == and ===?
Short Answer=== compares without type coercion. == may convert operand types before comparison.
Detailed ExplanationLoose equality follows coercion rules that can produce surprising results. Strict equality is generally easier to reason about when exact type and value should match.
5 == "5" // true
5 === "5" // false
Common follow-up: Be ready to explain coercion rather than only saying that triple-equals is “better”.
Learn this concept →
4. How does the JavaScript event loop relate to async code?
Short AnswerJavaScript runs synchronous work on the call stack while the runtime schedules asynchronous callbacks; the event loop coordinates when queued work can run.
Detailed ExplanationPromise reactions use the microtask queue, which is processed after the current stack becomes empty and before the next task such as a timer callback.
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
// A, D, C, B
Common follow-up: Promise callbacks normally run before a ready setTimeout task because microtasks are drained first.
Learn this concept →
5. What is the difference between a Promise and async/await?
Short Answerasync/await is syntax built on Promises that often makes asynchronous control flow easier to read.
Detailed ExplanationAn async function returns a Promise. await pauses that function's continuation until the awaited value settles without blocking the JavaScript runtime's whole thread.
async function loadUser() {
const response = await fetch("/api/user");
return response.json();
}
Common follow-up: Use try/catch around awaited operations when you need local error handling.
Learn this concept →