Convert Value to Boolean Equivalent with !!
The !!
operator in JavaScript is a double logical NOT used to convert a value to its boolean equivalent. Itโs a clever shorthand for coercing any value into a true
or false
.
๐ How It Works
- The first
!
negates the value โ turning truthy tofalse
, and falsy totrue
. - The second
!
negates it again โ flipping it back to the original boolean meaning.
โ Example
!!"hello" // true โ non-empty string is truthy
!!0 // false โ 0 is falsy
!!undefined // false โ undefined is falsy
!![] // true โ an empty array is truthy
!!null // false โ null is falsy
๐ง Why Use It?
- To normalize values to
true
orfalse
for conditional logic. - To cleanly check if a variable is defined or has a meaningful value.
- Often used in utility functions or when returning boolean flags.
โ ๏ธ Tip
Avoid using !!
in places where readability matters for beginners. Itโs concise but can be cryptic if youโre not familiar with coercion.