Double Exclamation Operator Example in JavaScript

Double Exclamation Operator Example in JavaScript

  1. JavaScript Double Exclamation Example With a False Output
  2. JavaScript Double Exclamation Example With a True Output
  3. False and True Values in Double Exclamation !! JavaScript

JavaScript double exclamation !!(not not) deliver outcomes same as Boolean expressions (True, False). Double Exclamation operator in JavaScript is a single repetition of unary logical operator !(not).

JavaScript Double Exclamation Example With a False Output

Following a short example using double exclamation operator represents an output in a Boolean value. The condition is that the true is not false, which is why !true results in a false value.

We create a variable and assigned an empty string with double exclamation falseOrTrue = !!"";. In the last step, document.write(falseOrTrue);, for variable output.

<script>
  //JavaScript code starts from here
  var falseOrTrue; 
  //In this case the given falseOrTrue variable is initlizes to store the result 
  //Double Exclamation operator checks the string is true or false
  falseOrTrue = !!"";
  //Now string is empty the result will be false
  document.write(falseOrTrue);
</script>

Output:

false

You can run the code on your own and can check the output. Now we will run another example to get a true output.

JavaScript Double Exclamation Example With a True Output

The following example contains the variable var falseOrTrue; as the above example’s variable. We create an empty object name with a double exclamation operator.

The stored value in the object is not empty. When we call the variable, it shows the value is true.

<script>
	//JavaScript code starts from here
	var falseOrTrue;
	//In this case the given object is empty 
  	//In this case the given falseOrTrue variable is initlizes to store the result 
	falseOrTrue = !!{
        items: 1
    };
    //Now object is not empty the result will be true
	document.write(falseOrTrue);
</script>

Output:

true

False and True Values in Double Exclamation !! JavaScript

Have a look at the table and see the !!value results.

 value       │  !!value
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 false       │   false
 true        │   true
 null        │   false
 undefined   │   false
 0           │   false
 -0          │   false
 1           │   true
 -5          │   true
 NaN         │   false
 ''          │   false
 'hello'     │   true

All the false values are false, and the true values are true in the !! operator.

Related Article - JavaScript Operator

  • Short-Circuiting in JavaScript
  • Ternary Operator in JavaScript
  • Operator Overloading in JavaScript
  • Elvis Operator in JavaScript
  • Bitwise XOR Operator in JavaScript
  • The Excamation Operator in JavaScript