php undefined和null,javascript中undefined与null区别

javascript中undefined与null区别

1.宽松比较时,两者相等,都表示空、没有、无等含义,undefined是未定义的变量,而null是没有

代码:

var s =(null==undefined);

console.log(s);

返回值是:true。

但是在===中是返回false

var s =(null===undefined);

console.log(s);

2.undefinod的出现4种情况:

//1.变量没有值

var i;

console.log(typeof (i));

//2.函数调用需要传值,但是没有传值时,函数返回值为undefined;

function f(x) {

return x;

}

var s = f();

console.log(s);

//3. 获取对象汇总不存在的属性时

var o ={};

console.log(o.p);

//4. 函数没有return时,函数的返回值时undefined

function f1(){}

var g =f1();

console.log(g);

3.获取一个不存在的对象时,值为nul1;例如:

// 3.获取一个不存在的对象时,值为null:例如:

var x =document.getElementById("xx");

console.log(x);

4.nul1的数据类型是object,undefined数据类型就是undefined。

5.nul1是表示‘无’的对象,转为数值时是0;Undefined 是表示‘无’的原始值,转为数值时NaN;

代码:

var n =Number(null);

console.log(n);

var u =Number(undefined);

console.log(u);

你可能感兴趣的:(php,undefined和null)