A closure in JavaScript is a function that has access to the variables in its enclosing scope, even after the enclosing scope has been closed. In other words, a closure is a function that "remembers" the variables from the scope in which it was created, even after that scope has been destroyed.
Closures are created when a function is created within another function. For example, the following code creates a closure:
function outerFunction() {
var name = "John Doe";
function innerFunction() {
console.log(name);
}
return innerFunction;
}
const innerFunction = outerFunction();
innerFunction(); // "John Doe"
In this code, the outerFunction() function creates a closure by creating the
innerFunction() function within its scope. The innerFunction() function has access to the
name variable from the outerFunction() scope, even though the
outerFunction() function has already been executed.
Closures can be used to create reusable functions that can access data from other functions. For example, the following code creates a closure that can be used to get the current user's name:
function getName() {
var name = localStorage.getItem("name");
function getUserName() {
return name;
}
return getUserName;
}
const getUser = getName();
console.log(getUser()); // "John Doe"
In this code, the getName() function creates a closure that returns the
getUserName() function. The getUserName() function has access to the
name variable from the getName() scope, even though the
getName() function has already been executed.
The getUser() function then calls the getUserName() function to get the current user's name.
Closures are a powerful tool that can be used to create reusable and efficient JavaScript code.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
A closure in JavaScript is a function that has access to the variables in its enclosing scope, even after the enclosing scope has been closed. In other words, a closure is a function that "remembers" the variables from the scope in which it was created, even after that scope has been destroyed.
Closures are created when a function is created within another function. For example, the following code creates a closure:
In this code, the
outerFunction()function creates a closure by creating theinnerFunction()function within its scope. TheinnerFunction()function has access to thenamevariable from theouterFunction()scope, even though theouterFunction()function has already been executed.Closures can be used to create reusable functions that can access data from other functions. For example, the following code creates a closure that can be used to get the current user's name:
In this code, the
getName()function creates a closure that returns thegetUserName()function. ThegetUserName()function has access to thenamevariable from thegetName()scope, even though thegetName()function has already been executed.The
getUser()function then calls thegetUserName()function to get the current user's name.Closures are a powerful tool that can be used to create reusable and efficient JavaScript code.