JavaScript高级程序一书中写到,undefined值是由null值派生来的,因此ESMC-262将他们定义为表面相等,所以 null == undefined 为true

执行以下程序,输出结果为()

var one;
var two = null;
console.log(one == two,one === two);

true false

变量one只声明未赋值,所以其值为undefined,
在使用“”对undefined和null进行比较时,不能将null和undefined转换成其他任何值,并且规定undefined == null返回结果为true,
而使用“
=”进行比较时,由于“===”需要严格区分数据类型,故undefined === null返回结果为False

Javascript规范中提到, 要比较相等性之前,不能将 null 和 undefined 转换成其他任何值,并且规定null 和 undefined 是相等的。null 和 undefined都代表着无效的值。

JavaScript高级程序一书中写到,undefined值是由null值派生来的,因此ESMC-262将他们定义为表面相等,所以 null == undefined 为true

posted @ 2023-11-10 23:06  龙陌  阅读(59)  评论(0)    收藏  举报