JavaScript: Double Not Operator?

Here’s something I discovered about JavaScript recently.

Operator ! The operator is a familiar face of JavaScript, appearing when we need to negate an existing value.

let thing = true
thing = !thing
thing === false // true

This is a Boolean value that is(true)and then inverts it to  (false).

This works also with non-Boolean value, as all JavaScript values are truthful or false.

let thing = "This string is truthy!"
thing = !thing
thing === false // true

This is how the ! Operator converts the string value (truthy), to its Boolean counterpart  false,We can also use the ! JavaScript allows you to use any operator to modify any JavaScript value. If the value is not truthful, it is cast as false, and vice versa.

Then it follows that !!This inversion can be performed twice.

let thing = "This string is truthy!"
thing = !!thing
thing === true // true

This is a quicker way to convert a Boolean value to a value. Use !!This will cause the value to be cast asfalse.

This could be done for the exact same result.

let thing = Boolean("This string is truthy!")
thing === true // true

What is the difference? According to my knowledge, there’s no difference!

While !! is more succinct, the argument could be made that it is more confusing syntactically than Boolean(value).

Exit mobile version