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/01 Report--
Editor to share with you what is the meaning of local variables in javascript, I hope you will gain something after reading this article, let's discuss it together!
In javascript, a local variable is a variable declared in the body of a function or a named parameter of a function; a local variable has a local scope, which means that a local variable can only be used within the function that defines it. Because local variables are defined within functions, variables with the same name can be used for different functions.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Local variables are variables declared in the body of a function or named parameters of a function. They have local scope, which means they can only be used in the functions that define them. Because local variables are defined within functions, variables with the same name can be used for different functions.
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.
Example:
There is no petName defined outside myfunction ().
Myfunction (); function myfunction () {var petName = "Sizzer"; / / local variabl document.getElementById ("demo1") [xss_clean] = "within the myfunction () function:" + typeof petName + "" + petName } document.getElementById ("demo2") [xss_clean] = "outside the myfunction () function:" + typeof petName
Output:
The above example illustrates the use of local variables. However, statements outside the function cannot reference a variable named petName without causing an error. This is because it has a local scope.
After reading this article, I believe you have a certain understanding of "what is the meaning of local variables in javascript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.