Interview Hub
Sign in

Q2: Explain closures with an interview-friendly example.

MidJavaScript 29

Answer

A closure is a function that retains access to variables from an outer (enclosing) scope even after that outer function returns. Example: a factory function counter(){ let n=0; return ()=>++n; } — each returned function closes over its own n. Interviewers often probe loops with var vs let in setTimeout callbacks to see if you understand per-iteration binding.

Having a tech or coding interview? Check 29 JavaScript & Node.js interview questions.

EditorJavaScript · local only