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 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 - Write a function once(fn) that lets fn run only the first time it is called.
Return a wrapper that keeps a called flag and the first result in a closure. On the first call, run fn and store the result; on later calls, return the stored result without running fn.
BeginnerQuick answerCoding1 min read - How can you create private variables in JavaScript using closures?
Declare the variable inside a function and return only functions that use it. The variable lives on in the closure, but no outside code can read or change it directly.
IntermediateQuick answer1 min read - Write a memoize function that caches the results of a pure function.
Wrap the function and store results in a Map keyed by its arguments. If the key exists, return the cached value; otherwise compute, store and return. It only works safely for pure functions.
IntermediateQuick answerCoding1 min read