The use of the triple exclamation mark (!!!)

Asked

Viewed 172 times

1

Consider the following code:

if (!!!window.customElements) {
    execPolyfill();          
}

What is the function of the 3 exclamation points before the expression window.customElements where only 1 would have the same effect?

  • 2

    Triple denial denies so denied!!!

  • @Jeffersonquesado I realized but what the reason to use this, it was not enough to use only 1?

  • I can only think that the intention is to improve readability.

  • 1

    It’s legibility, look at this related question in Soen. The value will always be the same as !window.customElements

  • @Isac it was nice to put this response of Soen here in English.

  • 1

    @Filipemoraes agree with you. I don’t know if whoever made the code heard of it "to take the boolean value of a falsy in javascript, use the double negation" and applied this principle to catch the reverse, hence the triple. We have two classes of denial operators involved here: falsy => boolean and boolean ==> boolean. The two leftmost denials (therefore the last to be operated on) are of the second type, whereas only the first negation (glued to the result of the function call) does the job of taking a falsy and turn into a boolean. How to deny a boolean is idempotent...

  • The question is related to this but it’s slightly different, so I don’t agree to be marked as [duplicated].

  • 1

    There’s no difference between !a and !!!a -> https://stackoverflow.com/a/25318045/6510304

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.