Closures in JavaScript - The easy way

mursalfk

Mursal Furqan Kumbhar

Posted on December 5, 2021

Closures in JavaScript - The easy way

Intro

Closure is a function inside another function that has access to outer function variables

Example ๐Ÿ‘‡

function foo() {
     let cat= '๐Ÿˆ'
     function bar() {
          console.log(cat, ' is a cat!')
     }
     bar()
}
foo()
Enter fullscreen mode Exit fullscreen mode

Explanation

In the above code, foo() created a local variable called cat and a function named bar(). The bar() function is an inner function that is defined inside foo().

Note that bar() don't have it's own variables. However, since inner function have access to variables of outer functions, bar() can access the variable cat.

Reason

Why do we learn itโ“
Answer โžก๏ธ These are used when one wants to extend the behavior such as pass variables, methods, etc. from an outer function to the inner function.๐Ÿ˜Ž

JavaScript is not a pure Object-Oriented Programming language, but you can achieve most of the OOP based behavior through closures.

Usage

Practical use case of closure is when you want to define a behavior and you want to attach some functions to manipulate those behavior with events.
I think this is the most obvious way of using closures and you should definitely use it to make your code shorter and development faster.
Image description
Let's see one of the best use case with code ๐Ÿ‘‡

Example

Suppose we want to add a button to a page to adjust the text size. One way to do this is to attach font-size in pixels (px) inside body element and then set the size of other elements on page using relative em units.

<body>
     ...
     <h1>Heading</h1>
     <p>Some Text</p>
     ...
</body>
Enter fullscreen mode Exit fullscreen mode
body {
     font-size: 12px;
}
h1 {
     font-size: 1.5em;
}
Enter fullscreen mode Exit fullscreen mode

Image description

Now, closure function is given below โฌ

function makeSizer(size) {
     return function() {
          document.body.style.fontsize = size + 'px'
     }
}
var size12 = makeSizer(12)
var size14 = makeSizer(14)

document.getElementById('size-12').onClick = size12
document.getElementById('size-14').onClick = size14
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“IKR, there are much better use cases of closures out there, but this was just to make the concept clear for young developers.

Why don't you all share your own use cases in the comments so that others may see live use cases of closures, in-action ๐Ÿ˜œ
Image description

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
mursalfk
Mursal Furqan Kumbhar

Posted on December 5, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Menu Animation in Action
javascript Menu Animation in Action

June 8, 2024

Dark light mode toggle
javascript Dark light mode toggle

March 19, 2023

Why CSS-in-JS?
javascript Why CSS-in-JS?

May 30, 2022

Closures in JavaScript - The easy way
javascript Closures in JavaScript - The easy way

December 5, 2021

ยฉ TheLazy.dev

About