1. Javascript variable scope
In JavaScript, variables are mainly divided into two types: local variables and global variables, and the corresponding scopes are also local scope and global scope.
1 Local variables and scope
Local variables are generally declared and used inside the function body:
function func(){ var i=12;//Local variables...}The scope of a local variable is used within the body of the function in which the variable is declared.
The declaration cycle of local variables is initialized when the function is called and executed, and is destroyed after the function call is completed.
2. Global variables and scope
Global variables are generally declared outside the function body:
var i=12;//global variable function func(){......}There is also a variable that is used directly without declaration, and defaults to a global variable:
function func(){ i=12;//Not declared (declared using var keyword), defaults to global variable}Global variables can be used in all scripts and methods in the current page, and their scope is in the current page script.
The declaration cycle 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 type of variables. The main data types in JavaScript are string, number, Boolean, object, etc.
console.log(typeof 'str');//stringconsole.log(typeof 23);//numberconsole.log(typeof false);//booleanconsole.log(typeof [1,3,12]);//objectconsole. log(typeof {name:'jack',age:12});//objectconsole.log(typeof new Date());//objectNote: Arrays and json objects in js are all of object data type.
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 expressing values, they can be expressed as null values, but they are still different in terms of data types.
console.log(typeof null);//objectvar persion = null;console.log(persion);//nullconsole.log(typeof undefined);//undefinedvar persion2;console.log(persion2);//undefined
The data type of null is object, and the data type of undefined is undefined.
When a variable is declared, the value is null, and the variable value is null; when a variable is only declared, without assignment, the value is undefined.
Let’s look at another set of comparisons:
console.log(null==undefined);//true values are equal console.log(null===undefined);//false types are not equal
It can be seen that when null and undefined represent values, they both represent empty; the data type of null is object, and the data type of undefined is undefined. Values that are declared without initialization are undefined.
The following is a supplement
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 100 alert(x); //Error is 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); //Also output 101, in Inside the method, there is no block-level scope, which is not possible in C#! ! ! for(var i=0;i<2;i++){ alert(i) } //The variable defined in the for loop is block-level scope alert(i); //Because i is a local variable, 2 is output} sayHello();</script>Note: Variables do not need to be declared with var before use. Such variables will be considered "global variables", but they are rarely used in this way.
About undefined and null
In the following situations, the value of the variable is undefined.
1. If a variable is defined but not assigned a value, the value of the variable is undefined.
2. The called method has no return value, and the returned value is undefined.
3. If the attribute value of the object does not exist, the return value is undefined, such as: document.ddd
Example1:
var xx;var yy=null;if(xx==yy){ alert('equal');}else{ alert('not equal');}The output results are equal because when performing the if judgment, the browser will judge the values of xx and yy. Since both have no specific values, they are considered to be false.
If the if judgment is replaced with ===[all equals sign], the output will not be equal! Because === means that the data types and values of xx and yy must be the same!
Example2:
var xx=10var yy='10';if(xx==yy){ alert('equal');}else{ alert('not equal');}The output is equal. If it is changed to ===, the output is not equal.
Example3:
var n='10';switch(n){ case 10: alert('number'); break; case '10': alert('string'); break;}Output string
The judgment in switch should consider the type
Summary: The judgment in if is to judge the value, regardless of the type.