Skip to content

Latest commit

 

History

History
80 lines (55 loc) · 2.13 KB

README.md

File metadata and controls

80 lines (55 loc) · 2.13 KB

Block Scope: Let vs Var

Eave very differently than var's when enclosed by curly braces.

Let variables will be confined to the braces where they were created: block scoping.

Var variables are hoisted outside of any curly braces they are in (except functions, this is a topic for later).

The example below illustrates how var & let variables are treated differently by JavaScript whenever a new block is created.

Index


Learning Objective

A set of opening and closing curly braces create a new block scope. let variables are stuck inside of the block scope where they were declared, var variables are not.

TOP


Study Snippet

let outer_let = 'outer let';
console.log('leaving outer scope');
{
    console.log('entering block scope');
    var inner_var = 'global var';
    let inner_let = 'inner let';
    console.log('leaving block scope');
};
console.log('re-entering outer scope');
inner_var = 'modified globally';
console.log('final state');

TOP


Helpful Links

Let's temporal dead zone. (from step 3):

TOP


Run-Time Sketches


TOP