课程: JavaScript: Scope

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

What are block-scoped variables?

What are block-scoped variables? - JavaScript教程

课程: JavaScript: Scope

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…

内容