[] == ![] 计算结果为真 [英] [] == ![] evaluates to true

查看:39
本文介绍了[] == ![] 计算结果为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么标题中给出的表达

I would like to know why the expression given in the title

[] == ![]

被评估为 true.

您不能将数组作为字符串进行比较.我明白了.如果

You cannot compare arrays as strings. I get that. If

[] == []

将评估为 false,因为引用不同.虽然如果我们有以下陈述.

will evaluate to false because the references are different. Though if we have the following statement.

var arr = []; 
arr == arr // this evaluates to true simply because references are the same.

为了使 A == B 返回 true,A 和 B 必须为 falsetrue.A == !B 为了返回 true A 可以是 true 并且 B 可以是 false 反之亦然 但在这种情况下,A 和B 是相同的值,所以我不明白.

In order A == B to return true either A and B have to be false or true. A == !B in order to return true A can be true and B can be false or vice versa but in this case, A and B are the same values so I don't get it.

推荐答案

![] 计算结果为 false 因为引用是真实的.[] 可以转换为一个错误的数字(在这种情况下为 0).因此:条件相等地通过.如果你做了====,那就是错误的.

![] evaluates to false because the reference is truthy. [] can be converted to an number (0 in this case) which is falsey. Therefore: the condition passes as equal. If you did === it would be false.

这篇关于[] == ![] 计算结果为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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