Not Operator
The ! operator inverts truthy and falsy values. Learn how to use NOT and double negation to write cleaner conditional logic in JavaScript.
The ! operator inverts truthy and falsy values. Learn how to use NOT and double negation to write cleaner conditional logic in JavaScript.
The ! (NOT) operator flips a boolean value. true becomes false, false becomes true.
!true; // false!false; // trueIt’s most useful in conditions:
const isLoggedIn = false;
if (!isLoggedIn) { console.log("Please log in"); // this runs}Reading it: “if NOT logged in”.
JavaScript values are either truthy or falsy. ! converts them to their boolean opposite:
!0; // true — 0 is falsy!""; // true — empty string is falsy!null; // true!undefined; // true!NaN; // true
!1; // false — non-zero numbers are truthy!"hello"; // false — non-empty strings are truthy![]; // false — arrays are truthy (even empty ones)!{}; // false — objects are truthy (even empty ones)!! converts any value to its actual boolean equivalent:
!!0; // false!!1; // true!!""; // false!!"hello"; // true!!null; // false!![]; // trueThis is useful when you need a real true or false instead of a truthy/falsy value — for example, when storing something in state or comparing with ===.
You don’t need !! in an if statement — JavaScript already coerces the value:
// These are equivalentif (!!value) { ... }if (value) { ... }Use !! when you explicitly need a boolean type, not just truthy/falsy behavior.