Home  >  Article  >  Web Front-end  >  Introduction to the difference between undefined and null in js_Basic knowledge

Introduction to the difference between undefined and null in js_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:36:451193browse

When a variable is declared but not assigned a value and a variable is not declared, it is undefined type data
JS variables must also be declared before use
If an undefined variable x is used, an error will be reported.
How to determine whether a variable is available:

The first method:
if(typeof x=='undefined')
var n=100;
n=new Date();
n= null;
null represents a clear known value, which is an empty object

Second method:

if(typeof(y)!='undefined' && v!=null)//You can also determine whether a variable is available

Third method:

var v;

if(v)

{//v will be converted to Boolean type and then judged. 

alert('available');

        }

else{

alert('unavailable');

         }

We rarely use the first and second methods, and usually use the third.                             

In

s, undefined and null are equal if == is used, and unequal if === is used.

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