LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 18 of 25

Closures Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Closures · Study material

Closures – Interview Questions

1. What is a closure?

A function bundled with references to the variables of the scope in which it was created, so it can use them even after that scope's function has returned.

2. Give a practical use of closures.

Private state (counters, bank accounts), function factories (makeMultiplier), memoization caches, once, debounce/throttle, event handlers and callbacks that need context, and the module pattern.

3. Why does a for loop with var and setTimeout print the same number?

All callbacks close over the same function-scoped variable, which holds its final value when they run. let creates a new binding per iteration; an IIFE can copy the value.

4. Do closures capture values or variables?

Variables. A closure sees the current value when it runs, not the value at the moment it was created.

5. If a factory is called twice, do the returned functions share state?

No. Each call creates a new environment. Functions returned from the same call do share it.

6. How do closures provide data privacy?

Variables declared in the outer function are not reachable from outside; only the returned inner functions can read or modify them.

7. What is a stale closure?

A closure that uses an old value – because it captured a copy, or a variable that was later replaced – while the code expects the current one. Common with timers and React hooks.

8. Can closures cause memory leaks?

They keep their captured variables alive as long as the closure is reachable. Long-lived closures (listeners, intervals, caches) that capture large data can hold memory unnecessarily.

9. What is the module pattern?

An IIFE (or factory) that keeps private variables in a closure and returns an object exposing only the public API.

10. How is memoization implemented with closures?

A higher-order function creates a cache and returns a function that checks the cache before calling the original function.

11. Does a closure capture this?

Regular functions get this from the call, not from the closure. Arrow functions have no own this and use the this of the enclosing scope, so they effectively capture it.

12. What is debouncing and how do closures help?

Delaying a function until calls stop for a given time. The returned function keeps the pending timer id in a closure so it can cancel and restart it.

13. What is the difference between a closure and a callback?

A callback is a function passed to be called later; a closure is a function that remembers its outer variables. Callbacks are often closures.

14. How do you reset private state correctly?

Assign to the captured variable (count = 0). Declaring let count = 0 inside the reset function creates a new local variable and leaves the private one unchanged.

15. What does an IIFE have to do with closures?

An IIFE creates a new scope immediately; functions returned or created inside it close over that scope – used for private state and for copying loop values before let existed.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links