Interview Q&A / JavaScript
JavaScript interview questions
Closures, the event loop, this, promises and the core language questions every frontend round asks.
Prefer rapid revision? Practise JavaScript short interview answers →
- What is a closure in JavaScript?
A closure is a function that remembers the variables from the place where it was created, even after that outer function has finished running.
BeginnerIn-depth2 min read - What is the difference between == and === in JavaScript?
=== (strict equality) is true only if both values have the same type and the same value. == (loose equality) first converts the values to a common type and then compares, which gives surprising results like 0 == "" being true. Use === by default.
BeginnerIn-depth3 min read - What is the difference between promises and async/await, and how do you handle errors with each?
async/await is cleaner syntax built on top of promises. An async function always returns a promise, and await pauses that function until a promise settles. With promises you handle errors using .catch(); with async/await you use try/catch around the await.
BeginnerIn-depth3 min read - What is the difference between var, let and const? Explain hoisting and the temporal dead zone.
var is function-scoped and is hoisted with the value undefined. let and const are block-scoped and are hoisted too, but cannot be used before their declaration line; that gap is the temporal dead zone. const also cannot be reassigned, though an object it holds can still be changed.
BeginnerIn-depth3 min read - How do tasks and microtasks work in the JavaScript event loop?
JavaScript runs one piece of code at a time. After the current code finishes, it runs all waiting microtasks (like promise callbacks) before the next task (like a setTimeout callback).
IntermediateIn-depth2 min read - How does the this keyword work in JavaScript?
this is decided by how a function is called, not where it is written. A method call obj.fn() sets this to obj; new sets it to the new object; call, apply and bind set it explicitly; a plain call gives undefined in strict mode. Arrow functions have no own this and use the surrounding one.
IntermediateIn-depth3 min read - What is the difference between debounce and throttle? Implement both.
Debounce waits until calls stop for a set time and then runs the function once, like search-as-you-type. Throttle runs the function at most once per time window no matter how often it is called, like handling scroll events. Both are built with a closure that holds a timer or a timestamp.
IntermediateIn-depthCoding3 min read - What are prototypes in JavaScript, and how does prototypal inheritance work?
Every object has a hidden link to another object called its prototype. When you read a property that the object does not have, JavaScript looks it up along this prototype chain. Classes are syntax built on top of this: methods live on the prototype and are shared by all instances.
AdvancedIn-depth3 min read