Closures in JavaScript

  • It has access to its own scope — variables defined between its curly brackets.
  • It has access to the outer function’s variables.
  • It has access to global variables.

Some Examples Using Closures:

function makeSizer(size) {
return function() {
document.body.style.fontSize = size + 'px';
};
}

var size12 = makeSizer(12);
var size14 = makeSizer(14);
var size16 = makeSizer(16);

--

--

--

Developer with 3 yrs of industrial experience in developing scalable web applications.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why does my setState() not work?

One of the most valuable topics in JavaScript is Truthy and Falsy values. Let’s have a look there.

Avoid Prop Drilling with React Context

6 Inspiring Frontend Challenges

Code

[2022] How to use subdirectories for hosting in Vue CLI’s Router (for Blank page problem)

Converting Codecademy to TypeScript 3: Learnings and Next Steps

Video game glitch showing Samual Jackson shouting and shaking angrily

5 Ways to Connect Redux Actions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.

More from Medium

Destructuring in JavaScript

Fetch JavaScript

Which is Better? Class Component Or Functional Component in React?