| by Arround The Web | No comments

Double Exclamation Operator Example in JavaScript

Everyone is familiar with the single exclamation mark (!) sign called the logical “not” operator, which is used to reverse a boolean value such as “!true” returns “false”, while “!false” returns “true”. The double exclamation mark (!!) symbols also called “the double bang”, or “double shots” change the value of a truthy or falsy into “true” or “false“. It’s a simple way to convert a variable to a boolean (true or false) value.

This study will define the double exclamation in JavaScript.

What is Double Exclamation Operator in JavaScript?

The double exclamation mark (!!) is not a JavaScript operator, it is a double, not (!) operator because the not (!) operator is used twice in the double exclamation operator (!!). The first (!) operator changes it to an inverted boolean value. The second (!) operator inverts the inverted boolean value. In other words, it is now the actual Boolean value of the object.

Falsy values in JavaScript

In JavaScript, the undefined, 0, null, NaN, and empty strings (‘’) are the false values.

Truthy values in JavaScript

The truth values of JavaScript are 1, a non-empty string, any non-zero number, arrays, objects, and so on.

Let’s look at the examples of double exclamation in JavaScript.

Example 1:

Create a variable “a” and assign a boolean value “false”:

var a = false

Use the double not(!) operator or double exclamation(!!) with the variable:

!!a;

The output gives the boolean value “false”:

In the above output, the value of variable “a” is first inverted to “true” then, the second (!) operator again inverts it into “false”.

Here, the below table represents the outcome of all the truthy and falsy JavaScript values using the Double Exclamation !! JavaScript:

Value !!Value
true true
false false
0 false
1 true
undefined false
null false
‘’ false
‘Linuxhint’ true

Let’s see how the double exclamation works on different values and data types.

Example 2: Applying (!!) on Boolean Values

Let’s check the effect of double exclamation (!!) on boolean values:

Example 3: Applying (!!) on Integer Values

Pass the integers 0 and 1 to the “console.log()” method with a double exclamation (!!) and will see the result:

Example 4: Applying (!!) on null or undefined Values

Let’s see the effect of double exclamation (!!) on the null or undefined values:

Example 5: Applying (!!) on String Values

Look at the effect of the double exclamation (!!) on an empty string and a string passing to the “console.log()” method:

We have compiled the essential instructions related to the double exclamation (!!) sign in JavaScript.

Conclusion

The double exclamation mark (!!) also known as “the double bang”, or “double shots” is the double not (!) operator that changes the value of a truthy or falsy statement into “true” or “false“. It is converted to an inverted boolean value using the first (!) operator. Then, the second (!) operator inverts the inverted boolean value. Finally, it gives the same results as boolean expressions (True, False). This study defined the double exclamation in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply