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 are the variables of javascript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what variables does javascript have". In daily operation, I believe many people have doubts about what variables javascript has. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what variables are there in javascript?" Next, please follow the editor to study!

Javascript has three kinds of variables, namely: 1, global variables, which are declared outside all functions; 3, local variables, which are variables declared in the function body or named parameters of the function; and 3, block-level variables, which are variables declared in the block and are only valid in the block.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

There are three kinds of variables in javascript, which are 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.

Three kinds of variables correspond to three kinds of scope: global scope, local scope (also known as function scope), block-level scope; the scope of variables is closely related to the way of declaration.

Variables declared with var have global and functional 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 window object, so they are not strictly global variables, only from the point of view 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 throughout the 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.

For block-level variables, their scope is the area between the beginning of the block-level variable declaration statement and the end of the block. The area between the beginning 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.

At this point, the study of "what are the variables of javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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