The Javascript logical NOT operator is represented by an exclamation point (!) and may be applied to any value in Javascript.
This operator returns a Boolean value, regardless of the data type it's used on.
The logical NOT operator first converts the operand to a Boolean value and then negates it.
The logical NOT behaves in the following ways:
The following example illustrates this behavior:
console.log(!false); // true console.log(!"blue"); // false console.log(!0); // true console.log(!NaN); // true console.log(!""); // true console.log(!12345); // false
The logical NOT operator can be used to convert a value into its Boolean equivalent.
By using two NOT operators in a row, you can simulate the behavior of the Boolean()
casting function.
The end result is the same as using the Boolean()
function on a value:
console.log(!!"blue"); // true console.log(!!0); // false console.log(!!NaN); // false console.log(!!""); // false console.log(!!12345); // true