Javascript local variables and global variables, javascript global variables

Source: Internet
Author: User

Javascript local variables and global variables, javascript global variables

In js, the function execution process is not only a local variable that overwrites the global variable, but also related to the declaration in the function. For example:

<Script> var a = 1; function test () {alert (a); // a is undefined! This a is not a global variable. This is because a local variable with the same name has been declared in function scope (the last 4th rows of the function body). // Therefore, global variable a is overwritten, this shows that Javascript performs a complete analysis on the definition of the entire script file before execution. Therefore, before the test () function is executed, // variable a in the function body is directed to the internal local variable. instead of pointing to external global variables. however, at this time, a only declares and has not been assigned a value, so undefined is output. A = 4 alert (a); // a is 4. Is there no suspense? Here, a is a local variable! Var a; // The local variable a declares alert (a) in this line; // a is still 4, because 4 has been assigned to a} test (); alert (a); // a is 1, which is not in function scope. The value of a is the value of the global variable. </script>


Global variables and local variables in javascript

First, you can directly run the script in <input type = "text" id = "dd" value = ""> program load.
Var a = document. getElementById ("dd"). value ;;
Id = "dd" cannot be found. If you place the script in <input……> To obtain the value.
If you want to define a as a global variable, the original place will not change in // call the global variable, (2) Add
A = document. getElementById ("dd"). value
You can.
The modified code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var a = 1;
Alert ();
Function DD (){
A = document. getElementById ("dd"). value;
Alert ();
A = 2;
Alert ();
}
</Script>
</Head>
<Body>
<Input type = "text" id = "dd" value = "">
<Input type = "button" value = "display" onclick = "DD ()">
</Body>
</Html>

In short, the problem you encountered here is the sequence of program running, rather than the global definition domain.

The running sequence of the Code is
Var a = 1;
Alert ();
Click <input type = "button" value = "show" onclick = "DD ()">
Run the code in DD ().

How to assign the value of a local variable to a global variable in javascript?

Var a = 0; // global
Function (){
Var B = 5; // local
A = B; // value assignment
}
Alert (a); // a is 5 at this time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.