Javscript中的null和undefined

1、null是JavaScript关键字,含义是“非对象”,它可以表示数字、字符串和对象是“无值”的。

var x = null;
typeof x ;//返回“object”

var x=null,y=null; x===y;//返回true

!null;//返回true

2、undefined表示更深层次的”空值“,它的值就是”未定义“。

它是变量的一种取值,表明变量没有初始化。

undefined和null不一样,它不是关键字,它是预定义的全局变量。

如果要查询对象属性或数组元素的值时返回undefined,则说明这个属性或元素不存在。如:

var x={name:'mlh'};x.age;
返回:undefined
var a=[];a[0];
返回:undefined

若果函数没有返回任何值,则返回undefined。如:function f(){};f();//返回undefined。

引用没有提供实参的函数形参的值也只会得到undefined。

3、null和undefined都表示”值的空缺“。

null和undefined都不包含任何属性或方法。

var x=null,y=undefined; x==y;
返回:true
var x=null,y=undefined;x===y
返回:false
原文地址:https://www.cnblogs.com/myboke/p/4817499.html