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';
!
means NOT.
- The first
!
(not) operator converts a value into a Boolean and reverses it, changing truthy values tofalse
and falsy values (e.g.,false
,0
,null
,undefined
,NaN
, or an empty string) totrue
.
!falsyValue; // true
!truthyValue // false
- 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 remainstrue
after the double negation, and if it is falsy, it becomesfalse
.
!!falsyValue; // false
!!truthyValue // true
Top comments (3)
Sharing article related to stop using double exclamation.
Great... It's a worthy tip
Thank you 😃