为什么null == undefined在javascript中为真 [英] why null==undefined is true in javascript

查看:2109
本文介绍了为什么null == undefined在javascript中为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们 alert(null == undefined)它输出到 true

这是什么原因。

这是用javascript进行硬编码还是有解释的。

Is this something that is hard coded in javascript or is there an explanation for this.

推荐答案

语言规范明确说明


如果x为null且y未定义,则返回true

If x is null and y is undefined, return true

我不知道语言设计过程的任何记录,解释了该决定的推理,但 == 有处理不同类型的规则,null和undefined都是无的东西,所以让它们相等就是直觉。

I'm not aware of any records of the language design process that explain the reasoning for that decision, but == has rules for handling different types, and "null" and "undefined" are both things that mean "nothing", so having them be equal makes intuitive sense.

(如果您不想要类型摆弄,请改用 === 。)

(If you don't want type fiddling, use === instead).

这篇关于为什么null == undefined在javascript中为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆