January 2020
Intermediate to advanced
548 pages
13h 36m
English
Let declarations are thankfully far simpler than var declarations. They will be scoped to their nearest environment (whether it is a block, a function, a module, or the global environment) and have no complicated hoisting behaviors.
Their ability to be scoped to a block means that a let declaration inside a block will not have an effect on the outer function scope. In the following code, we can see three different environments (scopes) with a respective place variable in each:
let place = 'outer';function foo() { let place = 'function'; { let place = 'block'; place; // => "block" } place; // => "function"}foo();place; // => "outer"
This demonstrates two things to us:
Read now
Unlock full access