DEV Community

Afia Anjum Preety
Afia Anjum Preety

Posted on

𝗗𝗼𝘂𝗯𝗹𝗲 𝗡𝗲𝗴𝗮𝘁𝗶𝗼𝗻 (!!)

Image description

Have you ever come across the !! or "not not" operator?
Well, I encountered it this morning.

In hashtag #JavaScript, using the double negation (!!) in front of a value or expression is a shorthand technique to convert a value into its boolean equivalent.

const falsyValue = NaN;
const truthyValue = 'JavaScript';
Enter fullscreen mode Exit fullscreen mode

! means NOT.

  • The first ! (not) operator converts a value into a Boolean and reverses it, changing truthy values to false and falsy values (e.g., false, 0, null, undefined, NaN, or an empty string) to true.
!falsyValue; // true
!truthyValue // false
Enter fullscreen mode Exit fullscreen mode
  • The second ! operator negates the result of the first !operation, converting the value back to its original boolean representation. If the original value is truthy, it remains true after the double negation, and if it is falsy, it becomes false.
!!falsyValue; // false
!!truthyValue // true
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
cezarytomczyk profile image
Cezary Tomczyk

Sharing article related to stop using double exclamation.

Collapse
 
tkirwa profile image
Tonny Kirwa

Great... It's a worthy tip

Collapse
 
2apreety18 profile image
Afia Anjum Preety

Thank you 😃