1. Javascript variable scope
In JavaScript, variables are mainly divided into local variables and global variables, and the corresponding scope is also the local scope and the global scope.
1 Local variables and scopes
Local variables are generally declared inside the function body:
function func(){ var i=12;//Local variables ...... }
The scope of local variables is used within the scope of the function body that declares this variable.
The declaration period of local variables is initialized from the execution of the function called and destroyed after the execution of the function call.
2. Global variables and scope
Global variables are generally declared outside the function body:
var i=12;//Global variablesfunction func(){ ...... }
There is also a variable that is directly used without a declaration, which defaults to global variables:
function func(){ i=12;//Not declared (declared with var keyword), default to global variable}
Global variables can be used in all scripts and methods in the current page, and the scope is in the current page script.
The declaration period of global variables is created when the variable is initialized and destroyed when the current page is closed.
2. Typeof keyword
The typeof keyword is mainly used to detect the data types of variables. The main data types in JavaScript include string, number, Boolean, object, etc.
(typeof 'str');//string (typeof 23);//number (typeof false);//boolean (typeof [1,3,12]);//object (typeof {name:'jack',age:12});//object (typeof new Date());//object
Note: Arrays and json objects in js are both object data types
3. null and undefined
null and undefined often appear in JavaScript, indicating that the value of a variable is empty or that a variable is not defined. When denoting a value, it can be represented as a null value, but it is still different in terms of data types.
(typeof null);//object var persion = null; (persion);//null (typeof undefined);//undefined var persion2; (persion2);//undefined
The data type of null is object, and the data type of undefined is undefined.
Variable declaration, the value is null, the value of the variable is null; variable is only declared, no assignment, and the value is undefined.
Let's take a look at another set of comparisons:
(null==undefined);//true values are equal(null===undefined);//false Types are not equal
This shows that when null and undefined represent values, they both represent empty; null's data type is object, and undefined data type is undefined. Values that are only declared but not initialized are undefined.
Below are supplements
Situation One
<script> var i; //Global variables//The method name is camel nomenclature//The variables in the method are local variables function sayHello(){ var x=100; alert(x); x++; } sayHello(); //Output 100alert(x); //An error has been reported, because x is a local variable and cannot be accessed</script>
Situation Two
<script> function sayHello(){ var x=100; if(x==100){ var y=x+1; alert(y); //Output 101} alert(y); //It also outputs 101. Inside the method, there is no block-level scope, in CIt's not possible in #! !for(var i=0;i<2;i++){ alert(i) } //The variables defined in the for loop are block-level scopealert(i); //Because i is a local variable, output 2 } sayHello(); </script>
Note: Var can be declared without using a var before use. Such variables will be considered "global variables", but they are rarely used like this.
About undefined and null
In the following cases, the value of the variable is undefined
1. The variable is defined but there is no assignment, the value of the variable is undefined
2. The called method has no return value, and the returned value is undefined
3. If the object's attribute value does not exist, the return value is undefined, such as:
Example1:
var xx; var yy=null; if(xx==yy){ alert('equal'); } else{ alert('No wait'); }
The output result is equal, because when making if judgment, the browser will judge the values of xx and yy, because neither of them has specific values and think they are both false.
If the if judgment is changed to ===[all equal to symbol], the output will be different! Because === means that the data types and values of xx and yy are required to be the same!
Example2:
var xx=10 var yy='10'; if(xx==yy){ alert('equal'); } else{ alert('No wait'); }
The output is equal, if replaced with ===, the output is not equal
Example3:
var n='10'; switch(n){ case 10: alert('number'); break; case '10': alert('String'); break; }
Output string
Types should be considered in the judgment in switch
Summary: The judgment in if is to judge the value without considering the type