为什么false ==“false”是假的? [英] Why false == "false" is false?

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

问题描述

我还在学习javaScript的基础知识,我不明白为什么会这样。

I am still learning the basics of javaScript and I don't understand why this happens.

有类型强制 false ==false 将转换为:

false == false //true

"false" == "false" //true

那么,为什么 false == false是假的?

推荐答案

你误解了类型转换规则 false 在比较之前不会转换为字符串。

You've misunderstood the type conversion rules. false doesn't get converted to a string before comparison.


如果Type( x)是布尔值,返回比较结果ToNumber(x)== y。

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

false 转换为一个数字,它给出:

false is converted to a number, which gives:

+0 == "false"

...然后......

… then …


如果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).

... false转换为一个数字,它给出:

"false" is converted to a number, which gives:

+0 == NaN

...这是假的。

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

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