Statement of JavaScript Variables learning notes

Source: Internet
Author: User

Declaration of variables

JavaScript uses keyword var to declare a variable, you can declare it after it is assigned, or you can assign a value at the same time as a declaration, and multiple variables declare commas (,) delimited. For example:

The code is as follows Copy Code
Declare first, then assign a value
var sex;
sex= "Male";
To assign a value while declaring
var age=22;
var name= "John";
Declaring multiple variables at the same time
var x=1,y=2,z=3;

When declaring multiple variables, separate them with commas (,), sex, age, name ... Called variable name, "male", 22, "Zhang three ..." is called a variable value.

JavaScript is a weakly typed language, and JavaScript automatically determines the data type based on variable content without declaring the data type.

JavaScript variable naming conventions: variables must begin with letters, $, and _, and cannot begin with numbers and other characters.

Note: JavaScript is case-sensitive, and variable age is not equal to age.

Use of variables

After the variable declaration, you can use the.

For example, two variables x and Y are declared:

The code is as follows Copy Code

var x=2;
var y=3; To get the value of X+y, you can:
var z=x+y;
document.write (z); Run the code and output 5.


Y:

Not assigned value variable
An unassigned variable is a variable that has been declared with the VAR keyword but has not been assigned a value.

In JavaScript, an unassigned variable has a default value of undefined, or undefined.

For example:

The code is as follows Copy Code

var x; x = undefined
alert (x);

Run Code, pop-up warning box, display undefined.

Note: Unassigned variables are not equal to undeclared variables. In JavaScript, referencing an unassigned variable with a value of undefined, referencing an undeclared variable will raise an error.

For example:

The code is as follows Copy Code

Alert (XYZ);

Run code, no pop-up warning box, open the Chrome Debugging tool (F12), you can see the following error has been raised:

Uncaught REFERENCEERROR:XYZ is not defined

Is "not caught reference error: XYZ is not defined".


One might ask what is the difference between the two declarations, and why there are two different ways of declaring them, which involves the scope of the variables in JavaScript. In JavaScript, the scope of a variable includes the global and functional levels.

Global variables can be declared outside the body of a function, regardless of which declaration method is used, variables declared outside the function body are global variables. Such as:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
var v = 1;

function foo ()
{
Alert (v);
}

W = 2;
function Bar ()
{
Alert (w);
}

Foo ();
</script>

Run Result: 1 2

In addition, a variable declared within a function will also be a global variable if the var keyword is not used. Such as:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
function foo ()
{
v = 1;
}

Foo ();
Alert (v);
</script>

Run Result: 1
Note, however, that in this case, to use a variable, you must first invoke the function that declares the variable to initialize the variable, such as Foo (), otherwise the "Variable v undefined" error will appear.

The global variable will exist as a property of the Window object because it can be accessed by window.$ ($ for variable name). Of course, it can be accessed directly by variable name. Here's why there are two ways to access it.

Variables declared within a function through the var keyword will be function-level variables whose scope is limited to within the function. Such as:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
function foo ()
{
var v=1;
Alert (v);
}

Alert (v);
</script>

Run Result: 1 variable "v" undefined

Through the above analysis, you can find that the key word var is to define the function level variables.

A careful friend can ask, what would be the result if the same variables were defined inside and outside the function? Such as:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
var v=1;
function foo ()
{
Alert (v);
var v=2;
}

Foo ();
</script>

Run Result: undefined
!!!!! Perhaps some people will be more depressed, v plainly defined in the function foo () body, why is undefined? This involves the parsing of JavaScript. According to experience, JavaScript's parsing process for a function's body variables is:
Search all the var keywords and place their variable declarations at the top of the function body, and the assignment and usage remain unchanged, so that the above JavaScript is actually equivalent to:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
var v=1;
function foo ()
{
var V;
Alert (v);
v=2;
}

Foo ();
</script>

By this analysis, the results above are obvious, because variables within a function have precedence over the precedence of global variables (as most programming languages do), the variable v inside the function overrides the global variable V, but because it simply declares but does not assign value when using the function's internal variable V, So the result is undefined.

If you still want to use the defined global variable V,window object in the method body, it is very useful now and can be accessed through WINDOW.V. Such as:

The code is as follows Copy Code

<script type= "Text/javascript" language= "JavaScript" >
var v=1;
function foo ()
{
alert (WINDOW.V);
Alert (v);
var v=2;
}

Foo ();
</script>

Operation Result: 2 undefined


Scope of variables

The scope of the variables in JS is divided into global variables and local variables, defined within the function called local variables, outside the function called global variables. ("Called global variable" outside the function is relative, and the premise discussed here is the variable explicitly declared with VAR, the variable defined in the function without VAR is the default global variable, of course, ignoring VAR declaration variable is not supported).

The code is as follows Copy Code

var glob = global variable declared outside 4;//function
function Fun () {
var height = 20; A local variable is declared in a function with Var
Weight = 50; A global variable is declared without Var in a function
}
Fun ();
alert (weight);

There is no block-level scope in JS, which is included with braces {}. In Java, there is. Write the following code in the Main method:

The code is as follows Copy Code

public static void Main (String ... args) {
for (int i=0;i<5;i++) {
}
{
int j=10;
}
int z = 20;
System.out.println (i); I am not visible, the grammar analysis times is wrong, that is, compilation does not pass
System.out.println (j); J not visible, the grammar analysis times wrong, that compile does not pass
System.out.println (z); Z Visible, Output 20
}

But if in JS:

The code is as follows Copy Code

for (Var i=0;i<5;i++) {
}
var obj = {name: "Lily"};
for (var attr in obj) {
}
{
var j=10;
}
alert (i);//Output 4, no block-level scope
alert (attr); Output name, no block-level scope
Alert (j);//Output 10, no block-level scope

This also illustrates the problem of avoiding the use of a For loop in the global scope to declare variables at the same time, otherwise polluting the global naming range.
Of course, the LET keyword declaration variable (see https://developer.mozilla.org/cn/New_in_JavaScript_1.7) is proposed in JS1.7 and only acts on the scope of the For statement.

The code is as follows Copy Code

for (let i=0;i<5;i++) {
Todo
}
alert (i)//run times wrong, hint I undefined

JS1.7 need to quote <script type= "application/javascript;version=1.7"/></script>
Ps:firefox2+ realized the JS1.7

Related Article

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.