What are Double shots ( !! ) in JavaScript ?

What are Double shots ( !! ) in JavaScript ?

What is !! in JavaScript ? where & how we can use it ? Let's discuss some feature of !! "Double Shots or Double Exclamation" in JavaScript.

So where it can be helpful, let's say we have some production grade application where we have some code that gives true or false condition as a result. for example like this.

const google = "google"
const fb = null

// some conditions which executes code blocks
if (google) {
  console.log('This execute block 1')
}

if (fb) {
  console.log('This execute block 2');
}

// Result is : This execute block 1

As per above condition only block 1 is executed, block 2 is null so it doesn't execute. Now here interesting part is that when "fb" is evaluated, now under the hood of JavaScript null is considered as a Falsy value not a False value.

Let's say we have authentication system & need value true or false as per condition given in code not truthy or falsy values, but if we use above implementation it gives us 'truthy' or 'falsy' value not 100% 'true' or 'false'.

So how to convert truthy value to actual true or falsy value to actual false. In some case we have to sure that result is 100% true or false (boolean). now we use ! operator to get value true or false like this example.

const google = "google"
const fb = null

if (google) {
  console.log('This execute block 1')
}

if (!fb) {
  console.log('This execute block 2');
}

// This execute block 1
// This execute block 2

Above code executes block 1 & block 2 because it it consider as true or false boolean value by Javascript not falsy or truthy.

If we want exact true or false value we use ! to convert it to boolean true or false, but it reverse the logic of the code. Now we have to use !! to convert value to actual true or false and still get same result as first example given above.

const google = "google"
const fb = null

if (google) {
  console.log('This execute block 1')
}

if (!!fb) {
  console.log('This execute block 2');
}

// This execute block 1
// so it is best practice for error free code & still get same result as ex. 1 by avoiding null values. 
// There is various use case for this & depend on various condition.

When we want to convert something to null or 0, we use '!' operator. First ! mark convert it to true, but we are 100% sure to get actual value, so another ! mark make it's result as it is & convert it to actual (boolean) false depend on use case.

if we use object like this example the problem comes with boolean objects. Every object is truthy, that means an instance of Boolean always resolves to true even when its actual value is false.

const price = 0

const hasPrice = new Boolean(price);

// good way is to make it boolean 
const hasPrice = Boolean(price);

// best way to use !! to convert true or false
const hasPrice = !!price;

This is the best practice to write error free code, also gives good performance in long run for production grade applications.

If you like this article feel free to share it so someone find it useful. Read my another article here. axay.hashnode.dev/design-tic-tac-toe-game-g..