What does !1 and !0 mean in Javascript?
Categories:
Understanding !1 and !0 in JavaScript: The Power of Logical NOT

Explore the meaning and practical applications of !1 and !0 in JavaScript, delving into type coercion and the logical NOT operator's role in boolean conversions.
In JavaScript, you might occasionally encounter expressions like !1 or !0. While they might look cryptic at first glance, they are fundamental examples of how JavaScript's logical NOT operator (!) interacts with truthy and falsy values. Understanding these simple expressions is key to grasping JavaScript's type coercion rules and writing more concise conditional logic.
The Logical NOT Operator (!)
The logical NOT operator (!) in JavaScript is a unary operator that takes a single operand and inverts its boolean value. If the operand can be coerced to true, the operator returns false. If the operand can be coerced to false, the operator returns true.
console.log(!true); // Output: false
console.log(!false); // Output: true
Basic usage of the logical NOT operator
Truthy and Falsy Values in JavaScript
Before we can fully understand !1 and !0, it's crucial to understand JavaScript's concept of 'truthiness' and 'falsiness'. In a boolean context (like when used with !), JavaScript implicitly converts non-boolean values into booleans. Values that convert to true are called 'truthy', and values that convert to false are called 'falsy'.
The following values are always falsy in JavaScript:
false0(the number zero)-0(the number negative zero)0n(BigInt zero)""(empty string)nullundefinedNaN(Not-a-Number)
All other values are considered truthy, including empty objects ({}), empty arrays ([]), and non-zero numbers.
![A flowchart illustrating JavaScript's truthy and falsy values. It shows a decision point 'Is value falsy?' leading to 'false' for falsy values (0, '', null, undefined, NaN) and 'true' for truthy values (1, 'hello', {}, []).](/img/abe40a7c-diagram-34.webp)
JavaScript Truthy and Falsy Values Flowchart
Deconstructing !1 and !0
Now, let's apply our understanding of the logical NOT operator and truthy/falsy values to !1 and !0.
What does !1 mean?
The expression !1 evaluates to false. Here's why:
1is a truthy value: In JavaScript, any non-zero number is considered truthy. So, when1is evaluated in a boolean context, it's coerced totrue.- The
!operator invertstrue: The logical NOT operator then takes thistruevalue and inverts it, resulting infalse.
console.log(Boolean(1)); // Output: true
console.log(!1); // Output: false
Demonstrating !1 evaluation
What does !0 mean?
Conversely, the expression !0 evaluates to true. This is due to:
0is a falsy value: The number zero (0) is one of the few explicitly falsy values in JavaScript. When0is evaluated in a boolean context, it's coerced tofalse.- The
!operator invertsfalse: The logical NOT operator then takes thisfalsevalue and inverts it, resulting intrue.
console.log(Boolean(0)); // Output: false
console.log(!0); // Output: true
Demonstrating !0 evaluation
!!value. For example, !!1 is true and !!0 is false. This is often preferred over Boolean(value) for conciseness, though Boolean() is generally more explicit and readable for beginners.const myValue = 5;
const isTruthy = !!myValue; // isTruthy will be true
const anotherValue = null;
const isFalsy = !!anotherValue; // isFalsy will be false
console.log(isTruthy); // Output: true
console.log(isFalsy); // Output: false
Using the double NOT operator for explicit boolean conversion