JavaScript NOT Operator can be used to find the flipped value of a boolean. It can be used to convert a true value to a false and vice-versa. This NOT operator can also be used on non-booleans to convert them to the reverse of their actual boolean value. A NOT operator can be used with another NOT operator to get the original value back.
Syntax:
!a
Return Type: Flipped boolean value.
Example 1: In this example, we will use the NOT operator on some boolean and other data types.
Javascript
console.log(! true ); console.log(! false ); console.log(! "1" ); console.log(! "" ); console.log(! null ); |
Output: We can see that the values converted to true are flipped and false is returned and vice versa.
false true false true true
Example 2: In this example, we will use two NOT operators on the same boolean and other data types.
Javascript
console.log(!! true ); console.log(!! false ); console.log(!! "1" ); console.log(!! "" ); console.log(!! null ); |
Output: When we applied the NOT operator two times we got the original boolean value back.
true false true false false
Supported Browsers:
- Chrome
- Edge
- Firefox
- Safari
- Opera
We have a complete list of JavaScript Logical Operators, to learn about those please go through JavaScript Logical Operator article.