Get the App
SLTechnology News&Howtos  ›  Development  › 

What does a local variable mean in javascript

Shulou Source: shulou.com Published: 2022-06-01 11:13:43 10月05日 Update

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!

Tags: Variable local function function global is in statement support valid region scope meaning different same between code parameter name meaning article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Apple NVidia MariaDB