Double Exclamation Mark JavaScript Notation

I find this to be a terribly obscure way to do a type conversion. The double exclamation mark in JavaScript basically means convert to Boolean, invert, then invert again. I documented this on my blog for quick reference as I keep running into it.

So you’re converting a value to a boolean, then inverting it, then inverting it again. Take a look at the three examples below that mean the same thing starting with the double exclamation mark.

// Really Confusing:
site.enable = !!webId;

// Less Confusing:
site.enable = (webId != 0) ? true : false;

// Easiest to understand in my opinion:
site.enable = (webId != 0);


Leave a Reply

Your email address will not be published. Required fields are marked *