If you are looking for a fancy definition, the scope defines the accessibility of variables. The bag of variables that we have access to at any given point of the code.
There are different scopes.
We have global, block, function, and module scope.
The global scope contains variables that everyone (all the other scopes) has access to. When the code runs in the browser, the global scope is the famous window object. The block scope represents variables inside a code block. The function scope contains variables inside the function block. And the module scope contains variables, functions, and classes defined inside a certain module.
Normally we have access to the global scope and our local scope (the block or function scope around us). What closures do is that they give us access to a different scope.
Accessing the enclosing scope via Closure
In its origins, the word “Closure” comes from enclosing. A concept from functional programming refers to sticking functions together with other things. They allow us to access the things around our functions, the surrounding scope (also called the lexical scope).
Hoisting is defined as lifting our declarations to the top of a function or global scope. That is a bit of a simplistic definition.
In reality, variables and function declarations are not moved anywhere.
This brings us to the next question…
Simply put, they are all housed to the top of their scope. Yet, let, and const are not initialized, while var variables are initialized with undefined. At the same time, remember, var and let can be declared without being assigned a value, while const must be assigned a value while declared.
Talking about global scope, have you ever wondered…
Long story short: We use this to access properties of the global or surrounding scope depending on how the function has been executed. Alone, “this” refers to the global scope where the code is being executed. In an object, “this” refers to the object’s properties. In a function, “this” refers again to the global scope.
And this brings me to the next question…
Besides the easy syntax, the main benefit of arrow functions is that they remove the uncertainty of what “this” refers to. In an arrow function, this always refers to the lexical context (scope) in which is being called. So no more confusion about the “context”.
Now let’s go a bit back in time with our next question.
This question is a bit 2010ish, but you might still get it today. The major difference between var and let is that the scope of a variable declared with let is local and only exists in the current code block. A variable declared with var exists in the global scope.
But before we get there, let’s talk about another difference.
Const, in exchange, can’t be either updated or re-declared (which has enormous implications when using TypeScript). If you want me to deep dive into that, drop me a line in the comments).
Ready for another “differences” question…
That’s right, the next question is...
Simply said, a prototype is a parent. A parent that you inherit your properties from.
This chain stops only when there are no more prototypes left. You can access the prototype by using the “prototype” property in any object :)
An illustration of the prototype chain.
We will finish with concepts from another fundamental pillar in the JS world: asynchronous programming, which brings me to the next question.
Back to callback and promises.
Let’s start by defining these two first. A call back is a function that is called back after something happened. Is like telling your friend, hey, when you are finished ordering tacos, take this function and run it for me, “call it back”.
To recap, a callback can be any function that will be passed as an input to another function.
Talking about promises…
Well, basically, none. Async/await only makes promises easier to read in a sequential way, one after the other. When you write asynchronous code with promises things can still look a bit nasty. You have all these statements that you don’t know when they close.
Async/Await makes it much easier for you to understand what happens, as you will understand what happens while reading things one by one.
And if you are interested in accelerating your journey to senior and you would love to be part of a community of developers with the same growth mindset, check our free training here.