What is the !! (not not) Operator in JavaScript?
Last Updated :
04 Oct, 2024
Improve
The !! (double negation) operator is a repetition of the unary logical "not" (!) operator twice. It is used to determine the truthiness of a value and convert it to a boolean (either true or false). Here’s how it works:
- The single ! (logical "not") inverts the truth value of a given expression:
- !false becomes true, because "false is not true."
- !true becomes false, because "true is not false."
- The double negation !! reverses this inversion, converting a value to its boolean equivalent:
- !!true returns true, because "true is not not true."
- !!false returns false, because "false is not not false."
In essence, !! forces a value into its corresponding boolean form, reflecting its inherent truthiness.
Example 1: This example checks the truthiness of the boolean value true.
// Declare a variable using let
let n1;
/* Checking the truthiness of
the boolean value true using !! */
n1 = !!true;
// Log the result to the console
console.log(n1);
Output
true
Example 2: This example checks the falsyness of the boolean value false.
// Declare a variable using let
let n1;
// Checking the falsiness of the boolean value false using !!
n1 = !!false;
// Output the result using console.log
console.log(n1);
Output
false
Example3: This example checks the truthyness or falsyness of a given string.
// Write Javascript code here
let n1;
// checking the truthiness of a given string.
n1 = !!"Javascript programming";
// Output the result (which will be true)
// using console.log
console.log(n1);
Output
true
Example 4: This example checks the truthyness or falsyness of a given object.
// Write Javascript code here
let n1;
// checking the truthiness
// of a given object.
n1 = !!{
articles: 70
};
// Output the result (which will be true) using console.log
console.log(n1);
Output
true