为什么0 ==“"”在JavaScript中是真的 [英] Why is 0 == "" true in JavaScript

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

问题描述

为什么JavaScript中的 0 ==为真?我在这里找到了一个类似的帖子,但为什么数字0类似于空字符串?当然, 0 ===为假。

Why is 0 == "" true in JavaScript? I have found a similar post here, but why is a number 0 similar an empty string? Of course, 0 === "" is false.

推荐答案

0 == ''

左操作数属于该类型Number。

右操作数的类型为String。

The left operand is of the type Number.
The right operand is of the type String.

在这种情况下,右操作数被强制转换为Number:

In this case, the right operand is coerced to the type Number:

0 == Number('')

导致

0 == 0






来自抽象等式比较算法(数字4):


如果Type(x)是Number而Type(y)是String,则返回
的结果比较x == ToNumber(y )。

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

资料来源: http: //es5.github.com/#x11.9.3

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

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