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

My Best Friend — Building an App using a Design System and Dog API

Event Loop in JavaScript

A Practical Introduction to TypeScript Class Decorators

Simplifying Angular Unit Testing

Implementing Routes with React Router DOM

Lightning Web Component v/s Aura v/s Flow with an Example

Build A Custom Like Button with HTML, CSS and Vanilla JavaScript

Software Architecture of Logbo

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

Special Concepts about JavaScript & MongoDB

Javascript so far…..ES6..!

padEnd() JavaScript

Basics of JavaScript for technical interviews