dev-resources.site
for different kinds of informations.
JavaScript Closure
Published at
7/19/2022
Categories
javascript
closures
Author
Bipon Biswas
Main Article
What is a Closure?
A closure is a function having access to the parent scope, even after the parent function has closed. Closures in JavaScript is a feature where an inner function has access to the outer function's variables.
A closure has three scope chains
- Has access to its own scope [the variable defined within it's curly]
- Has access to the variables of the outer functions
- Has access to the global variables
var a = 10;
function first_func(){
var b = 20;
function second_func(){
var c = a+b;
return c;
}
return second_func();
}
var sum = first_func();
document.write("The sum is " + sum + '<br>')
function temporary(){
let counter = 0;
return function(){
counter +=1;
}
}
const add = temporary();
console.dir(add)
add();
function temporary(){
let counter = 0;
return function(){
// counter +=1;
console.log("Death Closure")
}
}
const add = temporary();
console.dir(add)
add();
Articles
12 articles in total
Connect swagger in NodeJS server
read article
Create a REST API in Node.js
read article
Build a REST API with Node JS & Express for Beginner
read article
RxJS Map, Tap and Take in Angular
read article
RxJS Of and From in Angular
read article
Map, Filter, and Reduce in JavaScript
read article
Programming Fundamentals using JavaScript
read article
25 Useful tools for Frontend Developers
read article
JavaScript Async functions (async/await)
read article
JavaScript Closure
currently reading
Angular Dependency Injection
read article
Angular Components
read article
Featured ones: