What is Double Exclamation Marks in Javascript Mean?

Posted in Articles

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In some Javascript code, you might see the use of a double exclamation marks such as …

myAnswer = !! myVariable;

What does the double exclamation mark mean?  Is there even such an operator in Javascript?

Actually, it is two operators right next to each other.  Think of it as …

myAnswer = ! ( ! myVariable)

Now you can see that it is two negation operators (or “not” operators).  The negation operator returns a boolean.  The first negation operator converts myVariable to boolean.  But then you need the second negation operator to reverse the first negation.

So effectively, the double exclamation converts objects into boolean.