How to use JavaScript block-level scope
Today, the editor will share with you the relevant knowledge points about how to use the JavaScript block scope. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
JavaScript block level scope (Block Scope)
Variables declared with the var keyword do not have block-level scope and can still be accessed outside {}.
{
Var x = 2
}
/ / you can use the x variable here
Before ES6, there was no concept of block-level scope.
ES6 can use the let keyword to achieve block-level scope.
The variables declared by let are only valid within the code block {} where the let command is located and cannot be accessed outside {}.
{
Let x = 2
}
/ / the x variable cannot be used here
These are all the contents of the article "how to use JavaScript Block scopes". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.