【发布时间】:2022-01-17 02:20:51
【问题描述】:
!+(添加感叹号)在 JavaScript 中是什么意思?
为什么!+"000" 是真的?
为什么!+"0010" 是假的?
尝试:
!+"000" // true
!+"00010" // false
!+"0a0" // true
!+"0,0" // true
!+[0,0,0] // true
!+[0,1,0] // true
true+"000" // true000
我尝试过搜索:
- In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?
- What does "!--" do in JavaScript?
- What is the !! (not not) operator in JavaScript?
这里我看到了代码:JS - Check if string contain only 0
此信息在 Internet 上很难找到。
【问题讨论】:
-
这是两个独立的一元运算符
-
参见 What does this symbol mean in JavaScript? 和 MDN 上关于 expressions and operators 和 statements 的文档。可以组合运算符。您不会询问所有可能的组合,对吧?
-
true + "000"不使用一元+,而是使用二元。 specification 详细解释了它的作用。
标签: javascript