What does a !! in JavaScript mean?
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!
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
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.
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.
链接地址: http://www.djcxy.com/p/72860.html上一篇: 什么是 !! 在JavaScript?
下一篇: 什么是! 在JavaScript中意思?