Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript !! operators [duplicate]

Tags:

javascript

Possible Duplicate:
What is the !! (not not) operator in JavaScript?

I've seen operator ! used like !!. For example


var filter = !!(document.body.filters);

If I'm not wrong it's equivalent var filters = typeof document.body.filters != 'undefined'?

Is it a good practice to use !!?

like image 510
Tadas Šukys Avatar asked Feb 26 '26 21:02

Tadas Šukys


2 Answers

It's up to you. All !! does is "cast" its argument to a Boolean.

like image 172
jonchang Avatar answered Feb 28 '26 10:02

jonchang


It's a common way to convert any return type to boolean (usually to avoid compilation warnings). And second: no, checking if type is "undefined" is mandatory anyway and "!!" can not cover it.

like image 44
alemjerus Avatar answered Feb 28 '26 09:02

alemjerus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!