
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
"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 are a bit tricky, and what you saw in the last lesson might have confused the hell out of you.
00:00:06 So let me give you a quick example.
00:00:07 I'll create a function called init.
00:00:11 It'll be an arrow function.
00:00:13 And within it, I'll declare a variable called hobby.
00:00:17 hobby will be equal to something like learning JavaScript.
00:00:21 Then, below it, I'll create another function called displayHobby, which will be equal to an arrow function that'll simply console log that hobby from the
00:00:33 external function.
00:00:34 Immediately after, I can call displayHobby like this, and outside of it, I'll run the init function.
00:00:42 In this example, the init function creates a local variable called hobby and a function called displayHobby.
00:00:51 The displayHobby function is an inner function that is defined inside of the init function.
00:00:57 However, since inner functions have access to the variables from the outer functions, displayHobby can access the variable hobby declared in the parent
00:01:08 function init.
00:01:09 Now, if we return the displayHobby function declaration instead of calling it right here, and then we set it to another variable,
00:01:17 like my func is equal to init, and then we try to call this new function, you'll see that the output is the same, but with a twist.
00:01:27 The displayHobby inner function is returned from the outer function before being executed.
00:01:34 At first glance, it may seem unintuitive that this code still works.
00:01:38 In some programming languages, the local variables within a function exist only for the duration of that function's execution.
00:01:46 However, in JavaScript, functions form closures.
00:01:52 And the closure is the combination of a function and the environment within which that function was declared.
00:01:59 This environment consists of any local variables that were created in scope at the time that the closure was created.
00:02:07 In this case, the my func is the reference to the instance of the function display hobby created when init is run.
00:02:17 The instance of display hobby maintains a reference to its environment within which the variable hobby exists.
00:02:26 For this reason, when MyFunk is invoked, the variable hobby remains available for it to use.
00:02:35 Closures are a powerful feature and they work by default.
00:02:39 They're baked into JavaScript.
00:02:40 So you never have to say, hey, turn this into a closure or let's use a closure here.
00:02:46 It just works, enabling you to write more flexible and modular code.
00:02:51 Understanding it will just help you write better JavaScript code.
00:02:55 It's complicated, you will rarely use it, but just being able to wrap your head around it is helpful.
00:03:01 You might get asked about what a closure is and to explain it within a JavaScript interview.
00:03:06 so it's better to know it than not.
00:03:08 But then once you actually land your job, trust me, you will not concern yourself with closures ever again, unless you have to return an inner function,
00:03:16 and then set it to another variable, and then execute it, and then that function has to use the variables outside of its scope.
00:03:23 Ah, so many ifs, it'll rarely happen, but I still wanted to share it with you, so if you need it one day, or if somebody mentions it,
00:03:31 you know what they're talking about.
00:03:33 But with that in mind, don't stress yourself about this.
00:03:37 There are much more important things in the JavaScript language, and the one we'll cover in the next module is the most important of them all.
00:03:45 So take a bit of a break, go for a walk, and when you clear up your mind from all the closures, hoistings, and scopes, come back and let's dive right into it.