tags:

views:

138

answers:

5

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!

+5  A: 

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
+1  A: 

It converts to boolean

San4ez
+4  A: 

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
A: 

Its a "not not" arg

commonly used to convert (shortcut) string values to bool

like this..

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

Harley CF
@Harley: `if(!!'false') { alert('its true')}` still alerts `'true'`.
Daniel Vassallo
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
you'r right!, sorry the poor/short answer.. :)Daniel's answer is more complete
Harley CF
+2  A: 

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.

David Dorward