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..
An operator is a professional designation used in various industries, including broadcasting (in television and radio), computing, power generation and transmission, customer service, physics, and construction.
Definition: A Business Operator is the primary force driving a team working towards fulfillment of a company vision. This may be a dedicated team of contractors, but usually includes a mixture of contractors and employees.
Whitespace 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))))))))))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With