admin管理员组文章数量:1410712
I'am playing with some JavaScript and found something strange.
This code alerts "false" but gives no syntax errors. Someone could explain why adding one or even many !!!
after ===
is no resulting with any errors ?
var i = void 0;
var b = i ===! void 0 ? "true" : "false";
alert(b);//display false but no syntax errors..
I'am playing with some JavaScript and found something strange.
This code alerts "false" but gives no syntax errors. Someone could explain why adding one or even many !!!
after ===
is no resulting with any errors ?
var i = void 0;
var b = i ===! void 0 ? "true" : "false";
alert(b);//display false but no syntax errors..
Share
Improve this question
edited Oct 3, 2016 at 21:33
Merlin
asked Jan 7, 2014 at 19:09
MerlinMerlin
4,9272 gold badges34 silver badges53 bronze badges
2
-
1
!
is negating the following statement, even if the following statement is an!
– RienNeVaPlu͢s Commented Jan 7, 2014 at 19:11 -
1
And why should it?
!
is just a negation, so multiple!
will just invert each other – Alma Do Commented Jan 7, 2014 at 19:11
3 Answers
Reset to default 11Whitespace means nothing so it is
var b = (i === (!void 0)) ? "true" : "false";
which is
var b = (i === true) ? "true" : "false";
MDN Operator Precedence
!
is just a negation, and it is right-associative, unlike most other operators, so it will just negate whatever is in front of it
This is essentially equivalent to
var b = i ===(!void 0) ? "true" : "false";
So basically, you could have as many !
s in front of something as you want, and it wouldn't make a difference, so !!!!!!!!!!!!!false
, would evaluate to true, because it is the same thing as !(!(!(!(!(!(!(!(!(!(!(!(!false))))))))))))
See this table, which may help explain:
!0 // true
!!0 // false
!!!!!!0 // false, showing that !s are simply prefixes
! 0 // true, showing whitespace is irrelevant
0 === !0 // false
0 ===! 0 // false
0 ===!!! 0 // false
本文标签: javascriptWhat is the quotquot operator doingStack Overflow
版权声明:本文标题:javascript - What is the "===!" operator doing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744903689a2631492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论