AiredFree learning since 2009

JavaScript Closures Explained with Examples

A closure lets an inner function keep access to variables from the function that created it. It is one of the most common JavaScript interview topics.

A minimal closure

function counter() {
  let count = 0;
  return () => ++count;
}
const next = counter();
next(); // 1
next(); // 2

Why closures matter

  • Data privacy without classes.
  • Function factories and partial application.
  • Callbacks that remember their context.

A classic interview trap

Using var in a loop shares one variable across callbacks. Use let to create a new binding each iteration.

Written by

Admin

Tech Enthusiast | Blogger | Investor · Aired India

Real Estate. Crypto, Web3 Marketing. Business Growth. Audience Building. DM for Marketing Campaigns.

View profile