JavaScript Quiz: 20 Questions Developers Should Know
JavaScript powers almost every website on the planet, which makes it one of the most tested topics in technical interviews. The tricky part is that JavaScript has quirks โ hoisting, coercion, the event loop โ that trip up even experienced developers.
Below are 20 JavaScript quiz questions with clear answers and short explanations. Read through them to revise, then jump into the interactive quiz to test what stuck.
Core Concepts (Questions 1โ10)
Q1. What is the difference between `let`, `const`, and `var`?
โ Answer: `var` is function-scoped and hoisted; `let` and `const` are block-scoped. `const` cannot be reassigned.
๐ก `var` declarations are hoisted and initialised as `undefined`. `let` and `const` are hoisted too but sit in the "temporal dead zone" until the declaration line, so accessing them early throws a ReferenceError.
Q2. What is a closure?
โ Answer: A function that remembers the variables from the scope where it was created, even after that scope has returned.
๐ก Closures let inner functions keep access to outer variables. They power data privacy, function factories, and memoisation.
Q3. What does `typeof null` return?
โ Answer: 'object'
๐ก This is a long-standing bug in JavaScript kept for backward compatibility. `null` is a primitive, but `typeof null` still returns "object".
Q4. What is the output of `0.1 + 0.2 === 0.3`?
โ Answer: false
๐ก Floating-point numbers cannot represent 0.1 and 0.2 exactly, so the sum is 0.30000000000000004. Compare with a small epsilon instead of `===`.
Q5. What is hoisting?
โ Answer: JavaScript moving declarations to the top of their scope before execution.
๐ก Function declarations are fully hoisted (callable before they appear). `var` is hoisted as `undefined`. `let`/`const` are hoisted but not initialised.
Q6. What is the difference between `==` and `===`?
โ Answer: `==` compares after type coercion; `===` compares value and type with no coercion.
๐ก `0 == "0"` is true but `0 === "0"` is false. Prefer `===` to avoid surprising coercion bugs.
Q7. What does `this` refer to inside a regular function called on its own?
โ Answer: The global object (or `undefined` in strict mode).
๐ก The value of `this` depends on how a function is called, not where it is defined. Arrow functions instead inherit `this` from the enclosing scope.
Q8. What is the difference between `null` and `undefined`?
โ Answer: `undefined` means a variable has not been assigned; `null` is an intentional "no value".
๐ก `undefined` is the default for uninitialised variables and missing properties. `null` is a value you assign deliberately.
Q9. What is the result of `[] + []`?
โ Answer: An empty string ""
๐ก The `+` operator converts both arrays to primitives. An empty array becomes an empty string, so `"" + "" === ""`.
Q10. What is the difference between `map()` and `forEach()`?
โ Answer: `map()` returns a new array; `forEach()` returns undefined.
๐ก Use `map()` when you want a transformed array. Use `forEach()` for side effects where you do not need a return value.
When asked about `this`, always clarify *how* the function is called (method, standalone, arrow, or bound). That single distinction answers most `this` questions.
Async & Advanced (Questions 11โ20)
Q1. What is the event loop?
โ Answer: The mechanism that lets single-threaded JavaScript handle async work by moving completed callbacks from queues onto the call stack.
๐ก When the call stack is empty, the event loop pushes queued callbacks (from timers, promises, events) onto it, one at a time.
Q2. What is the difference between microtasks and macrotasks?
โ Answer: Promises use the microtask queue, which runs before the macrotask queue (setTimeout, setInterval).
๐ก After each task, all pending microtasks drain before the next macrotask runs โ which is why a resolved promise callback fires before a `setTimeout(โฆ, 0)`.
Q3. What is a Promise?
โ Answer: An object representing the eventual result of an async operation, with states pending, fulfilled, or rejected.
๐ก Promises replace callback nesting with `.then()`/`.catch()` chains and are the foundation for `async`/`await`.
Q4. What does `async`/`await` do?
โ Answer: `async` marks a function that returns a promise; `await` pauses it until a promise settles.
๐ก `await` makes async code read like synchronous code. Wrap awaits in `try/catch` to handle rejections.
Q5. What is the difference between `Promise.all` and `Promise.race`?
โ Answer: `all` waits for every promise (or the first rejection); `race` settles as soon as the first promise settles.
๐ก Use `Promise.all` for parallel work you all need; use `Promise.race` for timeouts or "first response wins" logic.
Q6. What is event delegation?
โ Answer: Attaching one listener to a parent element to handle events from many children via bubbling.
๐ก Delegation improves performance and works for elements added later, since the parent catches events that bubble up.
Q7. What is the difference between shallow copy and deep copy?
โ Answer: A shallow copy duplicates top-level values only; a deep copy duplicates nested objects too.
๐ก The spread operator and `Object.assign` are shallow. Use `structuredClone(obj)` for a true deep copy of most values.
Q8. What does the spread operator `...` do?
โ Answer: Expands an iterable into individual elements (arrays, function args) or copies object properties.
๐ก `[...arr]` copies an array, `{...obj}` copies an object, and `fn(...args)` spreads arguments into a call.
Q9. What is debouncing?
โ Answer: Delaying a function until a pause in rapid events, so it runs only once after activity stops.
๐ก Debouncing is common for search inputs and resize handlers to avoid firing on every keystroke or pixel.
Q10. What is the temporal dead zone (TDZ)?
โ Answer: The period between entering a scope and a `let`/`const` declaration, during which the variable cannot be accessed.
๐ก Referencing a `let`/`const` variable in its TDZ throws a ReferenceError, unlike `var` which returns `undefined`.
Do not compare floating-point numbers with `===`. Use `Math.abs(a - b) < Number.EPSILON` instead.
Frequently Asked Questions
Are these JavaScript questions good for interviews?
Yes. They cover the concepts most frequently asked in front-end and full-stack interviews: closures, hoisting, the event loop, promises, and coercion.
Do I need to know a framework like React to answer these?
No. These questions are about core JavaScript. Understanding them makes learning any framework much easier.
How should I practise JavaScript for interviews?
Read the concept, write small code snippets to confirm the behaviour in your console, then explain it out loud as if teaching someone. Active recall beats passive reading.
๐Related Articles
Every question is reviewed against the ECMAScript specification and MDN Web Docs.