JavaScript in 60 Seconds

Let

The let keyword is similar to var but with some key differences.

Let keyword

In JavaScript we can store a value in a variable with the var keyword.

if (5>3){ var x = 5; } x;

Now if we do the exact same thing with let

if (5>3){ let x = 5; } x;

Why is x undefined?

Let variables are scoped to the BLOCK

The block is marked by {}.

By contrast var scopes to the function.

Exercise: Now you try it.

// resolve the error if (5>3){ let x = 5; } x;

Nice job! On to the next lesson.