


In-depth understanding of variables and scope, undefined and null_javascript skills in JS
May 16, 2016 pm 04:57 PMSituation One
< ;script>
var i; //Global variable
//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 the method Internally, there is no block-level scope, not in C#! ! !
for(var i=0;i<2;i ){
alert(i)
} //The variables defined in the for loop are block-level scope
alert(i); //Because i is a local variable, output 2
}
sayHello();
</script>
Note: Variables do not need to be declared with var before use. Such variables will be considered "global variables". But it’s rarely used like this
About undefined and null
In the following situations, the value of the variable is undefined
1. The variable is defined but no value is assigned, then 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 result is equal, because when making if judgment, the browser will judge the value of xx and yy, because the two None of them have specific values, and they are considered 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=10
var 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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use JS and Baidu Maps to implement map pan function

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

How is the variable scope of a PHP function determined?

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

TypeError: Cannot read property '$XXX' of undefined in Vue, how to deal with it?

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function
