
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In this lesson, you'll learn about in JavaScript, a powerful concept that allows functions to access variables from their outer scope even after the outer function has finished executing.
are a fundamental concept in JavaScript that enable functions to retain access to their lexical scope, even when the function is executed outside of that scope. This is particularly useful when you have a function defined inside another function.
Here's a basic example to illustrate :
const outer = () => {
const outerVar = 'Hello!';
const inner = () => {
const innerVar = 'Hi!';
console.log(innerVar, outerVar);
};
return inner;
};
const innerFn = outer();
innerFn();
Normally, when you exit a function, all its variables “disappear” because nothing needs them anymore. But if you declare a function inside another function, the inner function can still be called later and access the variables of the outer function. For this to work, the outer function’s variables need to “stick around.” JavaScript handles this by keeping the variables alive instead of forgetting them, creating what is known as a "."
In other words, a gives you access to an outer function’s scope from an inner function. In JavaScript, are created every time a function is created, at function creation time.
const init = () => {
const hobby = 'Learning JavaScript'; // hobby is a local variable created by init
const displayHobby = () => { // displayHobby() is the inner function, a closure
console.log(hobby); // using variable declared in the parent function
};
displayHobby();
};
init();
const init = () => {
const hobby = 'Learning JavaScript'; // hobby is a local variable created by init
const displayHobby = () => { // displayHobby() is the inner function, a closure
console.log(hobby); // using variable declared in the parent function
};
return displayHobby;
};
const myFunc = init();
myFunc();
Running this code has the same effect as the previous example, but with a twist: the inner function is returned from the outer function before being executed.
At first glance, it may seem unintuitive that this code still works. In some programming languages, the local variables within a function exist only for the duration of that function's execution. However, in JavaScript, functions form closures. A closure is the combination of a function and the environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.
Closures are a powerful feature in JavaScript, enabling more flexible and modular code. Understanding closures will enhance your ability to write efficient and effective JavaScript programs.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 Closures in JavaScript are a powerful concept that allows functions to access variables from their outer scope even after the outer scope has finished executing.
00:00:12 But what does that actually mean?
00:00:14 Well, let me show you.
00:00:16 I'll create two functions, const outer is equal to a function, an error function in this case, within which I will create an outer variable,
00:00:27 const outer var saying something like hello.
00:00:31 Within this function, I'll also create an inner function, And this function will have its own inner variable, which will say hi.
00:00:42 Now from the inner function, of course, I can console log both the inner var as well as the outer var.
00:00:49 You know that because you understand how the scope works.
00:00:52 Now, if I try to execute the inner function from within the outer function, and I execute the outer function from the global scope,
00:01:00 we get both hi and hello.
00:01:03 But now check this out.
00:01:05 If instead of calling this inner function, I return it from the outer function, and then I re-declare it on the outside.
00:01:14 So const inner function is equal to the call of the outer function.
00:01:22 Do you understand what's happening here?
00:01:23 We're calling the outer function, which declares the inner function, and then it returns that inner function declaration as the return of the outer function,
00:01:33 which we then set to the inner function variable we create.
00:01:37 And now, we can call this inner function from the global scope.
00:01:42 That works something like this.
00:01:45 And if you do this, you still get both the hi and the hello.
00:01:49 Normally, when you exit a function, all of its variables disappear, because nothing needs them anymore.
00:01:55 But if you declare a function inside of another function, like we've done with this one right here, then the inner function can still be called later on
00:02:05 and access the variables of the outer function.
00:02:08 That's why we still get both hi from the inner function, which we're calling, but also hello, because for this to work, the outer function's variables,
00:02:18 the outer var, need to stick around.
00:02:21 And JavaScript handles this by keeping the variables alive instead of forgetting them, creating what's known as a closure.
00:02:29 In other words, a closure gives you access to an outer function scope from an inner function.
00:02:36 In JavaScript, closures are created every time a function is created at function creation time.