REVIEWED STARTER COLLECTION

JavaScript 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 Answer

let and const are block-scoped; var is function-scoped. const prevents reassignment of the binding.

Detailed Explanation

let/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
Learn this concept →

2. What is a JavaScript closure?

Short Answer

A closure is a function together with access to variables from the lexical environment where the function was created.

Detailed Explanation

An 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();
Learn this concept →

3. What is the difference between == and ===?

Short Answer

=== compares without type coercion. == may convert operand types before comparison.

Detailed Explanation

Loose 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
Learn this concept →

4. How does the JavaScript event loop relate to async code?

Short Answer

JavaScript runs synchronous work on the call stack while the runtime schedules asynchronous callbacks; the event loop coordinates when queued work can run.

Detailed Explanation

Promise 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
Learn this concept →

5. What is the difference between a Promise and async/await?

Short Answer

async/await is syntax built on Promises that often makes asynchronous control flow easier to read.

Detailed Explanation

An 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();
}
Learn this concept →