javascript - 为什么 ![] 不是真的?

标签 javascript

当我测试这个时,我只是在玩弄 nodejs 和 chrome 的控制台:

[] == true // false
![] == true // false
!![] == true // true

怎么会?是不是错了?

最佳答案

参见 ECMAScript standard :

11.4.9 Logical NOT Operator ( ! )

The production UnaryExpression : ! UnaryExpression is evaluated as follows:

  1. Let expr be the result of evaluating UnaryExpression.
  2. Let oldValue be ToBoolean(GetValue(expr)).
  3. If oldValue is true, return false.
  4. Return true.

9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

  • undefined → false
  • null → false
  • Boolean → The result equals the input argument (no conversion).
  • Number → The result is false if the argument is +0, -0, or NaN; otherwise the result is true.
  • The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
  • Object → true

数组是一个对象。

关于javascript - 为什么 ![] 不是真的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11025870/

相关文章:

javascript - 下拉列表不会与下拉按钮垂直对齐

javascript - 加载图像后更改文本

javascript构造函数参数太多

javascript - 如何根据链接标题属性的不同措辞更改链接的文本?

javascript - 如何修复类里面的未定义错误?

javascript - 当 <div> 标签的类名看起来很奇怪时,如何更改它?

Javascript:按值从 Json 中删除元素

javascript - console.log javascript [功能]

javascript - 如何在 zone.js 中找到错误源 - 'filter' 未定义

javascript - 影响单元格大小的表格边框