Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "===!" operator doing?

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..
like image 738
Merlin Avatar asked Jan 07 '14 19:01

Merlin


People also ask

What type of job is an operator?

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.

What does an operator do in a business?

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.


2 Answers

Whitespace means nothing so it is

var b = (i === (!void 0))  ? "true" : "false";

which is

var b = (i === true) ? "true" : "false";

MDN Operator Precedence

like image 156
epascarello Avatar answered Oct 09 '22 23:10

epascarello


! 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))))))))))))

like image 30
scrblnrd3 Avatar answered Oct 09 '22 22:10

scrblnrd3