The !! operator in JavaScript converts any value to a boolean: true for non-zero, non-empty strings, and objects, and false for 0, null, undefined, NaN, and empty strings. It is used to cast to boolean, invert boolean and implement lazy loading.
The meaning of !! operator in JS
!!
operation in JS operator is called the double negation operator in JavaScript. It does the following:
Convert any value to a boolean
Take any value as input and the !!
operator will convert it Is a Boolean value:
true
. 0
, null
, undefined
, NaN
, and the empty string, the result is false
. Usage Examples
Here are some examples of usage of the !!
operators:
<code class="js">console.log(!!0); // false console.log(!!1); // true console.log(!!''); // false console.log(!!"Hello"); // true console.log(!!null); // false console.log(!!undefined); // false console.log(!!NaN); // false</code>
Purpose
!!
operator is mainly used for the following purposes:
true
to false
and vice versa. The above is the detailed content of What does !! in js mean?. For more information, please follow other related articles on the PHP Chinese website!