Home Web Front-end JS Tutorial In-depth understanding of variables and scope, undefined and null_javascript skills in JS

In-depth understanding of variables and scope, undefined and null_javascript skills in JS

May 16, 2016 pm 04:57 PM
js null undefined variable scope

Situation One

Copy code The code is as follows:

&lt ;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
Copy code The code is as follows:

<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:

Copy code The code is as follows:

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:
Copy code The code is as follows:

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:

Copy code The code is as follows:

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

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

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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 Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

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? How is the variable scope of a PHP function determined? Apr 16, 2024 pm 04:51 PM

How is the variable scope of a PHP function determined?

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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? TypeError: Cannot read property '$XXX' of undefined in Vue, how to deal with it? Nov 25, 2023 pm 12:14 PM

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 create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

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

See all articles