- 01
What is a closure in JavaScript?
Question codejavascriptfor (var index = 0; index < 3; index += 1) { setTimeout(() => console.log(index), 0) }A closure is a function together with access to the lexical environment in which it was created. It enables private state, callbacks, and function factories, but can also retain memory longer than expected.
Answer codejavascriptfor (let index = 0; index < 3; index += 1) { setTimeout(() => console.log(index), 0) } // Each callback closes over its own block-scoped index. - 02
How do var, let, and const differ?
var is function-scoped and hoisted with an undefined value, while let and const are block-scoped and remain in the temporal dead zone before declaration. const prevents rebinding, not mutation of the referenced object.
- 03
How does the JavaScript event loop handle asynchronous work?
Synchronous code runs on the call stack, while completed asynchronous operations enqueue work. Microtasks such as promise callbacks run before the next task such as a timer, which affects observable execution order.
- 04
What is the difference between Promise.all and Promise.allSettled?
Promise.all rejects as soon as one input rejects, making it suitable when every result is required. Promise.allSettled waits for every input and returns each outcome, which is useful for independent operations.
- 05
When should shallow and deep copying be considered?
A shallow copy duplicates only the top-level container, so nested objects remain shared. A deep copy recursively duplicates supported values, but the correct choice depends on identity, performance, and the data types involved.
- 06
How would you reduce unnecessary JavaScript work in a web page?
Ship less code, split by real interaction boundaries, avoid repeated layout work, and remove expensive render or event patterns. Use browser profiling to identify the actual main-thread cost before optimizing.
Programming Languages
JavaScript Interview Questions and Answers
Study JavaScript interview questions covering scope, closures, the event loop, promises, modules, object behavior, and frontend performance.
Continue preparing
Related interview guides
Need delivery capability?
