From the course: JavaScript: Scope

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

What are block-scoped variables?

What are block-scoped variables? - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

What are block-scoped variables?

- [Instructor] The last item we need to cover is block scope variables. They were introduced with ES6 and uses the let and const keywords. If we go back to local variables they are available within the function they are declared in. But with block scope variables they are only available inside of the block they are declared. For example, if we take a look at this code and this code, they are only available within this block of code. So, if we declare a variable inside of an if statement in a function, this variable won't be available in the function outside of the if statement because it is block scoped, inside of the if statement and so on, so forth if you have multiple, nested functions. When we want to declare a variable, which one do we use, let or const? What is the different in between them? In short, let allows to mutate or update the variable as you please. While const is immutable, or you can't change the value…

Contents