In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article mainly focuses on how to understand the variable declaration in JavaScript. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Variable
In ECMAScript, a variable can hold any type of data (either a string or an array or something else), that is, "loose", a variable is just a placeholder used to distinguish, a total of three keywords var, const, and let are used to declare variables (var is available in all versions of ECMAScrip, the latter two are only available in ES6 and later).
(I) varvar a bank / set the value while only defining the var breadth bank / definition
It is important to note that the setting of the value can be overridden, but it is not recommended
Var a = "hello" a = "hi"
ECMAScript variables are "loose", so you can use a statement to initialize declarations for different data types, of course, inserting and wrapping are not necessary, but be sure to separate different variables with commas.
Var a = "hello", bread12, cased false 1) about the scope of variables declared by var function test () {var a = "shanxi";} test (); / / call function console.log (a); / / ReferenceError: an is not defined// error: an is not defined
When a variable is defined with var, if it is inside the function, the variable will be destroyed when the function exits and can no longer be called. Here, an is defined using var inside the function test. After calling the function test, an is destroyed, resulting in an error.
When the keyword var is omitted, the variable defined in this way becomes a global variable (though this is not recommended, too many global variables will make the program difficult to maintain)
Function test () {a = "sichuan";} test (); / / call the function console.log (a); / / sichuan2) var declares promotion (hoist)
As follows, the result is undefined without reporting an error, because variables declared with the var keyword are automatically promoted to the top of the function scope. That is, it will be seen by ECMAScript as
Function test () {console.log (a) var axiom 12;} test () / / undefined
That is, it will be seen by ECMAScript as
Function test () {var a; console.log (a) axiom 12;} test () / / undefined
This "promotion" raises the declaration of all variables to the top of the function scope.
(2) let
Let and var act similarly, except that the scope of let declaration is block scope, and the scope of var declaration is function scope
For example, {} in the following if statement is a "block" rather than a function scope.
If (true) {var b = "zhang" console.log (b); / / zhang} console.log (b); / / zhang
Replace it with let:
If (true) {let b = "zhang" console.log (b); / / zhang} console.log (b); / / ReferenceError: b is not defined
At this point, b cannot be referenced outside the if (outside the scope of the block that defines b).
!!! Note: the block scope is a subdomain of the function scope (the former is not necessarily the latter).
!!! Note: the scope restrictions that apply to var are equivalent to let (except that one scope is function scope and the other is block scope).
For repeated declarations, var will not report an error but let will report an error. See the following example:
Var axiom 24; / 5let bread2: let baccalaure4; let SyntaxError: if Identifier 'b'has already been declared// is running here, it has already reported the wrong let baccalaureate 8 let console.log (b); var axiom 24 has already been declared// var Avalanche 12 Victorius 5 let console.log (a); / / 5let bread2 transmissionlet baccalaure4 let SyntaxError: if Identifier 'b'has already been declared// runs here, it has already reported the wrong let baccalaureate.
With regard to nesting use, JavaScript records the identifier used for variable declaration and the scope of the block in which it resides, so no error is reported when nesting is used (because there is no duplicate declaration in the same scope).
Var a = "shanxi" console.log (a); / / shanxiif (true) {var a = "sichaun"; console.log (a); / / sichuan} let bread3 console.log (b); / / 3if (true) {let bread5; console.log (b); / / 5}
So, the only difference between let and var is that they determine that the relevant scope of the declared variable exists.
Unlike var, the variables declared by let will not be promoted in scope, a phenomenon known as "temporary dead zone" console.log (a); var Asp2; var console.log undefinedconsole.log (b); and let bread3, and the referenceError: Cannot access 'b'before initialization2) global declaration.
Variables declared by Var in the global scope automatically become properties of the window object, but let does not
Var console.log (window.a) / / alet breadth 10 console.log (window.b); / / undefined
However, the variable declared by let still occurs in the global scope (otherwise it would not be "undeifned"), and the variable will also exist in the life cycle of the page, so it is necessary to ensure that the page will not declare the same variable repeatedly.
(3) Const
Basically the same as let, except that when using const, variables must be initialized at the same time, and an error will be reported when trying to modify variables declared by const
Const n = "zhang"; n = "li"; / / TypeError: Assignment to constant variable.const axiom 12; Const a' has already been declared 9; no repeatable declaration / / SyntaxError: Identifier'a 'has already been declared
Of course, const's restrictions on declaration apply only to references to the variables it points to, that is, if it is an object, modifying the object's internal properties will not violate const restrictions.
Const house= {}
House.name= "myhouse"
So, after the advent of let and const, many developers no longer use var a lot and use let and const more, so that variables have a clear scope, declaration location, and immutable values.
Thank you for your reading. I believe you have some understanding of "how to understand variable declarations in JavaScript". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.