November 8, 2023
true and falsy values to
false. It is a common technique used to coerce a value to a boolean type.
What is double negation?
!) is used to invert a boolean value. When it is applied twice (
!!), it effectively converts a value to its boolean equivalent without changing its truthiness. This is known as double negation.
Truthy and falsy values
true in a boolean context, and falsy values evaluate to
Every other value is truthy, including objects, non-empty strings, and arrays.
Using double negation
Here’s how you can use double negation:
Double negation can be particularly useful in conditional statements and when you need to ensure a value is a true boolean:
Comparison with Boolean constructor
An alternative to double negation is using the
Boolean constructor. However, using
!! is generally preferred for its brevity and speed:
Be cautious with double negation when dealing with numbers and strings:
How to Truncate Date in MySQL