2

Possible Duplicates:
myVar = !!someOtherVar
What does the !! operator (double exclamation point) mean in JavaScript?

Came across this line of code

strict = !!argStrict

... here and wondered what effect the !! has on the line? Pretty new to JS!

Community
  • 1
  • 1
Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148
  • 3
    We need a Javascript version of http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php-closed! – kennytm Sep 23 '10 at 21:03

5 Answers5

8

It converts your value to a boolean type:

var x = '1';
var y = !!x;

// (typeof y === 'boolean')

Also note the following:

var x = 0;
var y = '0';       // non empty string is truthy
var z = '';

console.log(!!x);  // false
console.log(!!y);  // true
console.log(!!z);  // false
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
4

It converts the value to a value of the boolean type by negating it twice. It's used when you want to make sure that a value is a boolean value, and not a value of another type.

In JS everything that deals with booleans accepts values of other types, and some can even return non-booleans (for instance, || and &&). ! however always returns a boolean value so it can be used to convert things to boolean.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

It is a pair of logical not operators.

It converts a falsey value (such as 0 or false) to true and then false and a truthy value (such as true or "hello") to false and then true.

The net result is you get a boolean version of whatever the value is.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

It converts to boolean

San4ez
  • 8,091
  • 4
  • 41
  • 62
0

Its a "not not" arg

commonly used to convert (shortcut) string values to bool

like this..

if(!!'true') { alert('its true')}

  • `if(!!'false') { alert('its true')}` still alerts `'true'`. – Daniel Vassallo Sep 23 '10 at 21:03
  • To add to Daniel's comment, non-empty strings in javascript are always considered "truthy." A null or empty-string will be considered "falsy" when casting. Perhaps you meant to write `if(!!true) { alert('it is true')}` ? – Funka Sep 23 '10 at 21:08
  • you'r right!, sorry the poor/short answer.. :) Daniel's answer is more complete – Harley Cabral Sep 23 '10 at 21:23