Is there a '! !' operator?

During a code review, I’m seeing something like the following.

{ 
   caffeineConsumer: !!body.patientHealthHistory.historyItems.find(item => item.symptomID === HistoryTypesEnums.CAFFEINE),
   alcoholConsumer:  !!body.patientHealthHistory.historyItems.find(item => item.symptomID === HistoryTypesEnums.ALCOHOL)
}

Is there really a ‘!!’ operator and I just don’t know about it or is this really saying NOT NOT?

Bang bang operator :wink:

1 Like

It basically forces the value that follows to a strict true or false value. The first ! inverts the value and coerces it to a true/false, the second switches it BACK to the original value’s truthiness or falsiness.

A longhand version of the same thing?

Boolean(body.patientHealthHistory.historyItems.find(item => item.symptomID === HistoryTypesEnums.CAFFEINE) )
2 Likes

There is a ! operator, Boolean NOT. !! is literally just two boolean NOT operators. It’s normally used as a short version of Boolean(someValue), ie safely coercing some value to true/false:

Haha, snap

2 Likes

Nicely said, very short and to the point. Hahaha!

Good example I posted on StackOverflow.

function hasDecimal(num){
    return !!(num % 1);
}

console.log(hasDecimal(2)); // false
console.log(hasDecimal(2.32423)); // true
2 Likes