Reply to comment

Cast any Javascript Value to a Boolean with Double Exclamation

It's a way to cast any variable to a boolean.

var x = true;

!!x === true; // this is a fact

Sometimes, we do a check on an object:

if (window.foo) {
  ...
}

However, if we assign a variable like this:

var isFoo = window.foo;

isFoo will now contain a reference to window.foo. 
If you want isFoo to contain the truthyness of 
window.foo, you do this:

var isFoo = !!window.foo;

Reply

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options

12 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.