JavaScript, unlike Java and C, is a non-type, weak-detection language. It does not need to declare variable types, and we can assign various types of data to the same variable as long as they are in the form of a value.
First, the type of JS variable and declaration method
var i=100;//number type
var i= "variable";//string type var i=false;//boolen type var i={x:4};//object type var i=[1,2,3];//array type the first three kinds are basic types, The latter two are reference types 1. The reference type can be added property Method 2 for its relative base type. When a base type is copied to another variable, a new value is created on the variable object, and the value is copied to the location assigned to the new variable, and the reference type refers to the same object. JS in the variable declaration of the explicit Declaration and implicit declaration. VAR i=100;//Explicit declaration
i=100;//Implicit declaration
Variables that are explicitly declared by using the var keyword in a function are local variables, not the VAR keyword, and the global variables are declared using the direct assignment method.
When we use access to a non-declared variable, JS will error. And when we assign a value to a variable that is not declared, JS does not give an error, but instead it thinks we are going to implicitly declare a global variable, which we must pay attention to. Second, JS variable scope
The scope of JS variables can be divided into: "Global variables" and "Local variables"
"Global variables": Declaring variables outside of a function
"Local variable": declares a variable in the body of the function and is accessible only within the body of the current function, such as: function () {var a = 0;}
Note: Variables that are declared are generally not the VAR keyword, and the directly assigned variable is a global variable
Example:
Here are some small examples to familiarize yourself with.
1. Function test () {
A = 30;
var B = 20;
}
Test ();
Console.log ("a=" +a); It's obvious here that A is a global variable
Console.log ("b=" +b);//b is a local variable, so in the function test out of the box, the hint is undefined
2.
var a = 1;
function Test () {
Console.log ("a=" +a); Here A is undefined
The variables declared in the/* function are defined throughout the function. If there is a defined variable inside the function, even if it is output before the definition, it executes the later definition statement and then determines the output, so that the declared variable will work in the whole function. */
var a = 2;
}
Test ();
3.
For the comparison of two small examples:
var A; function Fun () {a = "global";} console.log (a);//Output undefined
var A; function Fun () {a = "global";}
Fun (); Console.log (a);//Output Global
For these two small examples, the only difference is that one executes the fun function and one is not executed;
If it is
var a;console.log (a);//Because a is defined but not initialized, the output undefined
and function Fun () {...} The initialization operation is in the scope of the fun function, and if you do not execute the fun ()
4, the function domain takes precedence over the global domain, therefore the local variable a will overwrite the global variable a
var a=1;
function Main () {
var a=2;//local Variables
Console.log (a);//2
}
Main ();
Console.log (a);//1
5. JavaScript does not have a block-level scope
function Test () {
for (var i = 0; i < 3; i++) {
i=0,1,2, exit loop when last execution to i=3
}
Console.log (i);//3
}
Test ();
Equivalent
function Test () {
var i;
for (i = 0; i < 3; i++) {
i=0,1,2, exit loop when last execution to i=3
}
Console.log (i);//3
}
Test ();
JS variable and its scope