Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of the triple exclamation mark

Tags:

javascript

Looking through the source code of one of our projects, I've found some amount of places where we're using three exclamation marks in conditional statements, like so:

if (!!!someVar) {     // ... } 

Now, I understand that this isn't some kind of rarely used operator, it's just three negations in a row, like !(!(!someVar))). I don't understand what's the use of it - in my opinion it can safely be replaced with single exclamation mark. Following are my attempts to find a case when !!!a isn't equal to !a (taken straight from the google chrome console):

var a = '' "" !!!a === !a true a = 'string' "string" !!!a === !a true a = null null !!!a === !a true a = 12 12 !!!a === !a true a = {b: 1} Object {b: 1} !!!a.c === !a.c // a.c is undefined here true a = [] [] !!!a === !a true a = [1,2] [1, 2] !!!a === !a true 

Am I missing some rare (or obvious) case?

like image 913
aga Avatar asked Jan 16 '14 06:01

aga


People also ask

What does 3 exclamations mean?

In formal text it means you didn't do enough with the words to sell the reader in being exclaimed. In informal writing it means you're really trying to sell the statement at shocking.

Can I use three exclamation points?

3. In informal writing, multiple exclamation points are sometimes used to indicate stronger emphasis or emotion. However, in formal writing only one is necessary. 4.

What does it mean when someone uses a lot of exclamation points?

Overuse of the exclamation point, though (e.g., using them on back to back sentences) can backfire. For example, when a person uses “too many” exclamation points, they're perceived as desperate. When used properly, though, exclamation marks can be particularly valuable for communicating gratefulness.

What does 3 exclamation marks mean in Javascript?

So the third use (!! x) turns the "falsey" value into a true boolean. ... with that in mind, the third exclamation point makes a TRUE negation of the original value (a negation of the "true boolean" value).


Video Answer


1 Answers

There is no difference between !a and !!!a, since !!!a is just !!(!a) and because !a is a boolean, !!(!a) is just its double negation, therefore the same.

like image 52
Sophiα2329 Avatar answered Sep 22 '22 10:09

Sophiα2329