In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about what variables refer to in javascript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In javascript, a variable is a container that temporarily stores values, which can store numbers, text, or some complex data, etc., while the variable name is the label affixed to the container, through which you can find the variable so that you can read and write the value it stores.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
What is a variable?
Literally, a variable is a variable; programmatically, a variable is a container for temporarily storing values that can store numbers, text, or some complex data, and so on. The variable name is the label affixed to the container, through which you can find the variable so that you can read and write the values it stores.
For example, two boxes, to distinguish between them, one is represented by box1, the other is represented by box2, of course, you can also use any name to distinguish, this box1 is the name of the box, that is, the name of the so-called variable.
How are variables defined?
Define variables (declare variables). Any variable must be defined before it can be used. If multiple variables are defined, different storage space is allocated for each variable.
In javascript, you can use the keyword: const,var,let to define variables, syntax:
Keyword variable name
Example:
Var name;var name, age, sex
1. Use var to define variables
The variables defined by var can be modified, and if not initialized, undefined will be output without error.
/ / print console.log (a) without definition; / / print undefined// initialization var a = "aaa"; console.log (a); / print aaa// modification variable var a = "bbb"; console.log (a); / / print bbb// loop to detect block-level scope for (I = 0; I < 5; iTunes +) {console.log (I) / / print 0 1, 2, 3, 3, 4, setTimeout (function () {/ / define the function internal variable console.log (I); var d = "locald";}, 1000) / / print 0re1jue 2jingo 3jin4} / access internal variables / / set timer, because it takes a second for d to be defined setTimeout (function () {console.log (d)}, 1001); / / error is reported, not defined
2. Use const to define variables
Variables defined by const cannot be modified and must be initialized. Const defines a constant.
/ / undefined console.log (a); / / an error is reported. There is no definition, so you need to define / / initialize const a = "aaa"; console.log (a); / / print the aaa// modification variable const a = "bbb"; console.log (a); / / error, an already assigned value, unable to modify / / loop, detect block-level scope for (I = 0; I < 5; iCool +) {console.log (I) / / print 0JEI 1JEI 3JEI 4setTimeout (function () {/ / define the function internal variable console.log (I); const d = "locald";}, 1000); / / print 0JJ 1JI 3JI 4} / / access the internal variable / / set the timer, because it takes a second before d is defined setTimeout (function () {console.log (d)}, 1001); / / error is reported, not defined.
3. Use let to define variables
Let is a block-level scope, and the definition of let inside the function has no effect on the outside of the function.
/ / undefined console.log (a); / / error report, you need to define / / initialize let a = "aaa"; console.log (a); / / print the aaa// modification variable let a = "bbb"; console.log (a); / / print a has been declared / / looped to detect the block-level scope for (I = 0; I < 5; iCool +) {console.log (I) / / print 0meme 1d2 setTimeout 4 setTimeout (function () {/ / define the function internal variable console.log (I); let d = "locald";}, 1000); / / print 0meme 1meme, 3Jing 4} / / access internal variables / / set timer, because it takes a second before d is defined setTimeout (function () {console.log (d)}, 1001); / / error is reported, not defined.
Pay attention to one thing, pay attention to the print of for loop, which is very different from var.
This highlights the benefits of block-level scope
Thank you for reading! This is the end of this article on "what variables in javascript refer to". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.