Javascript Closures
- Get link
- X
- Other Apps
The previous line is hard to understand because its about closures which is a very critical concept in Javascript.In this blog we will take a look at Javascript closures and will try to make it simplke for you all.
function outerFunction() {
let count = 0;
function innerFunction() {
count++
return count
}
return innerFunction
}
const innerFunc = outerFunction()
console.log(innerFunc())
console.log(innerFunc())
console.log(innerFunc())
//1
//2
//3
Let us more example of inner functions
function outerFunction() {
let count = 0;
function plusOne() {
count++
return count
}
function minusOne() {
count--
return count
}
return {
plusOne:plusOne(),
minusOne:minusOne()
}
}
const innerFuncs = outerFunction()
console.log(innerFuncs.plusOne)
console.log(innerFuncs.minusOne)
Closures make JavaScript very powerful and versatile. Closure helps in data encapsulation, remove redundant code, etc.
In other languages this won't work but in JavaScript it works.
- Get link
- X
- Other Apps

Comments
Post a Comment