【问题标题】:Why is an empty array equal to false? [duplicate]为什么空数组等于假? [复制]
【发布时间】:2020-10-17 16:52:24
【问题描述】:

我正在阅读 You Don't Know JS 一书的“类型和语法”部分,我很难理解这一点。

为什么空数组等于false

console.log(false == []); // true

【问题讨论】:

标签: javascript type-coercion


【解决方案1】:

当您执行[] == false 时,会在后台调用Array.prototype.toString 方法,并将该空数组作为其this 上下文,它返回空字符串"",而空字符串在JavaScript 中是一个虚假值。

【讨论】:

  • @hev1 The specification 称其为“空字符串”因为只有一个
  • @user4642212 感谢您通知我。
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 2017-12-20
  • 2020-08-22
  • 2013-10-17
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多