- Reduced Name Collision - Different scope let use reuse variable names reducing variable name collisions, also known as namespace collisions.
- Secure - Scope takes care of variables accessible by only pre-defined parts of code. This enforces security.
When a function is invoked, there is always an object container around that function. This object container is its context and 'this' keyword points to that context. Context is not set when a function is declared, but rather where it is invoked.
The difference between these three comes down to two factors:
- Assignment Mutability
- Supporting nonfunction block scope.
An assignment is an act of pointing that variable in memory, as all the variables act as pointers.
Mutability is whether or not a variable can be reassigned once it has initially been assigned something.
The block of code where a variable or argument is declared determines its scope.
When using let or const, an argument’s or a variable’s scope is always the actual block in which it is declared.
What do you think would be output if I replace 'var' with 'let'.
It gives an error. Why?
Because calling var in an 'if' or 'loop' will assign the variable to the scope of the nearest enclosing function.
While for let, scope is always the actual block in which it is declared.
When a function is invoked, there is always an object container around that function.
This object container is its context and 'this' keyword points to that context.
Context is not set when a function is declared, but rather where it is invoked.
The defining feature is: It is Single-Threaded. Suppose a Stack. The work currently in progress in the stack owns the thread.
The rest of the works are lined up in a Queue waiting for their chance. Why? Because Single Threaded.
When the stack gets empty, an event loop takes work from the Queue and places it in the Stack.
Just to tell you how the engine works.
Every year an update is released with new features. Syntactical or Semantical.
Changes to obtain clearer syntax are often referred to as - Syntactic Sugar.
I keep posting such informative threads on Twitter too. Make sure to follow me - twitter.com/sushrutkm