PrepPilotPractise free

Core JavaScript interview questions

Scope, closures, this, prototypes, promises, event loop

17 questions · ES2024 · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is the difference between var, let, and const?

var is function-scoped and hoisted, while let and const are block-scoped. const also prevents reassignment. Only var is initialized to undefined immediately; let and const are not accessible before their declaration due to the temporal dead zone.

Explain what a closure is in JavaScript.

A closure is a function that remembers the variables from its lexical scope even after that scope has finished executing. This allows functions to access variables defined outside their immediate scope.

How does this behave in arrow functions compared to regular functions?

Arrow functions don't have their own this; they inherit it from the surrounding scope. Regular functions have their own this, which depends on how they're called.

What does === do differently from ==?

=== checks for strict equality without type coercion, so both value and type must match. == allows type coercion, which can lead to unexpected results.

Intermediate questions

Describe the temporal dead zone and how it relates to let and const.

The temporal dead zone is the period between entering a scope and the actual declaration of a let or const variable. During this time, accessing the variable throws a ReferenceError because it's not initialized yet, even though it's hoisted.

How can you create a private variable in JavaScript using closures?

You can define a variable inside a function and return another function that accesses it. The inner function forms a closure, keeping the variable private and only accessible through the returned function.

How does prototype inheritance work in JavaScript?

Objects in JavaScript inherit properties and methods from their prototype. When you access a property, JavaScript looks up the prototype chain until it finds it or reaches the end. ES6 classes use prototypes under the hood for inheritance.

What are the differences between ESM and CommonJS modules?

ESM uses import and export syntax, supports static analysis, and is natively supported in browsers and modern Node.js. CommonJS uses require and module.exports, is synchronous, and is standard in older Node.js. ESM is preferred for modern projects.

How do you handle errors in async/await code?

I use try/catch blocks inside async functions to handle errors. Alternatively, I can attach a .catch handler to the returned promise. This ensures errors are caught and handled gracefully.

Explain the difference between microtasks and macrotasks in the event loop.

Microtasks include promise callbacks and MutationObservers, and are executed after the current call stack but before the next macrotask. Macrotasks include setTimeout, setInterval, and I/O, and are handled after microtasks. This affects the order of async code execution.

Why is immutability important in JavaScript, and how do array methods help?

Immutability prevents unexpected side effects by avoiding mutation of data. Methods like map, filter, and reduce return new arrays, making it easier to write predictable and maintainable code.

How does JavaScript handle type coercion in comparisons?

JavaScript converts values to a common type when using ==. For example, 0 == false is true because both are coerced to 0. This can lead to bugs, so I prefer === for strict comparisons.

Senior questions

How would you debug a bug caused by variable hoisting in a large codebase?

See the answer in the app, free

Describe a scenario where improper this binding caused a production bug. How did you fix it?

See the answer in the app, free

How do you ensure robust error handling in a promise-heavy Node.js application?

See the answer in the app, free

How would you optimize array processing for immutability and performance in a large dataset?

See the answer in the app, free

How do you migrate a Node.js project from CommonJS to ESM?

See the answer in the app, free

More JavaScript & Node.js topics