Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the scope of JS variables

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the scope of JS variables". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the scope of JS variables".

The scope of a variable refers to the readable and writable valid scope of the variable in the script code, that is, the area in the script code where the variable can be used. Before ES6, the scope of variables is mainly divided into global scope and local scope (also known as function scope); after ES6, the scope of variables is mainly divided into global scope, local scope and block scope. The corresponding scope variables are called global variables, local variables and block-level variables. Global variables are declared outside all functions; local variables are variables declared in the body of the function or named parameters of the function; block-level variables are variables declared in the block and are valid only in the block.

The scope of a variable is closely related to the way it is declared. Variables declared with var have global and local scopes and no block-level scope; variables declared with let and const have global scope, local scope, and block-level scope.

Note: strictly global variables belong to the properties of the Window object, but the variables declared by let and const do not belong to the Windows object, so they are not strictly global variables, and they are global variables only in terms of their scope.

Because var supports variable promotion, the global scope of var variables is valid for script code throughout the page, while let and const do not support variable promotion, so the global scope of let and const variables refers to the entire area from the beginning of the declaration statement to the end of the script code of the entire page, while the area before the declaration statement is not valid. Similarly, because var supports variable promotion, while let and const do not, local variables declared with var are valid for the entire function, while local variables declared with let and const are valid from the beginning of the declaration statement to the end of the function. It should be noted that if the local variable and the global variable have the same name, then in the function scope, the local variable will cover the global variable, that is, it is the local variable that plays a role in the function body; outside the function, the global variable works, the local variable is invalid, and a syntax error will occur when referencing the local variable. The area between the start of the block and the block-level variable declaration statement is a temporary dead zone, where the block-level variable is not valid.

In addition, in non-strict operation mode, variables do not need to be declared, and these undeclared variables belong to global variables no matter where they are used. It is generally not recommended that variables be used directly without declaration, as this may result in errors that are not easy to find.

Var v1 = "JavaScript"; / / global variable let v2 = "JScript"; / / global variable let v3 = "Script"; / / global variable scopeTest (); / / call function function scopeTest () {var lv = "aaa"; / / local variable var v1 = "bbb"; / / local variable let v2 = "ccc" / / local variable if (true) {let lv= "123"; console.log ("block-level output lv=" + lv); / / 123} console.log ("function body output lv=" + lv); / / aaa console.log ("function body output v1 =" + v1) / / bbb console.log ("function body output v2 =" + v2); / / ccc console.log ("function body output v3 =" + v3); / / Script console.log ("function body output v4 =" + v4); / / undefined, v4 is a global variable, assignment is later, var has variable promotion, so the value is undefined} var v4 = "VB" / / global variable console.log ("function external output lv =" + lv); / / report ReferenceError error console.log ("function body output v1 =" + v1); / / JavaScript console.log ("function body output v2 =" + v2); / / JScript console.log ("function body output v3 =" + v3); / / Script console.log ("function body output v4 =" + v4); / / VB

The above script code declares four global variables, three local variables, and one block-level variable. Outside the scopeTest function, variables v1, v2, v3 and v4 are global variables; in the scopeTest function body, lv and v2 are global variables; in the if judgment block, lv is a block-level variable. We can see that the local variables v1 and v2 have the same name as the global variables v1 and v2. In the body of the scopeTest function, the local variables v1 and v2 are valid, so in the body of the function, the output of these two variables is that bbb and ccc; are valid in the global variables v1 and v2, respectively, so outside the function, the output results of these two variables are JavaScript and JScript, respectively. In addition, the block-level variable lv and the local variable lv have the same name. In the if judgment block, the block-level variable lv is valid, so the output result in the block is 123.When outside the block, the local variable lv is valid, and the output result of the lv variable is aaa. In addition, the global variables v3 and v4 are not overwritten in the function body, so the output of v3 is the value of the global variable, so the v3 output result is Script both outside and inside the function, while the v4 variable assignment is after the function call, so the v4 output result in the function body is undefined, while the output outside the function is after the declaration, so the result is VB. Lv is a local variable, so accessing the function outside the body will report a ReferenceError error.

Summary: block-level variables cover local variables in the block, local variables cover global variables in the function body, and global variables that are not covered are valid both inside and outside the function.

Thank you for your reading, the above is the content of "what is the scope of JS variables". After the study of this article, I believe you have a deeper understanding of what the scope of JS variables is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report