The ! operator (logical NOT)
The ! operator is called logical negation (NOT) and it inverts the logical value of an expression. It first converts its operand to boolean and then flips the result, so it always returns exactly true or false, whatever the type of the input value.
Theory
TL;DR
!trueisfalse,!falseistrue.- If the value is truthy, the result is
false; if it is falsy, the result istrue. - There are only eight falsy values:
false,0,-0,0n,'',null,undefined,NaN. Everything else is truthy. []and{}are objects, and therefore truthy:![]and!{}both givefalse.!!valueis the idiom for explicitly casting any value toboolean.- The result of
!is always of typeboolean, which makes the operator handy in checks likeif (!user).
Quick example
const isLoggedIn = false;
if (!isLoggedIn) {
// !false becomes true, so the block runs
console.log('User is not authenticated');
}
const input = '';
console.log(!input); // true (an empty string is falsy)
console.log(!!input); // false (the same value as a boolean)The rule and behaviour across types
The rule is short:
!true // false
!false // trueIf the value is truthy, it becomes
false. If the value is falsy, it becomestrue.
A full set of examples for values of different types:
!true // false
!false // true
!0 // true (0 is a falsy value)
!1 // false (1 is a truthy value)
!"" // true (an empty string is a falsy value)
!"hello" // false (a non-empty string is a truthy value)
!undefined // true
!null // true
!NaN // true
![] // false (an array is truthy)
!{} // false (an object is truthy)Pay attention to the last two lines: an empty array and an empty object are not falsy, because they are object references. Emptiness has to be checked explicitly, for example with arr.length === 0.
Double negation !!
The doubled operator shows up very often in JavaScript:
!!valueIt is used to cast any value to boolean: the first ! inverts, the second one flips it back, and what is left is a clean logical type.
!!'hello' // true
!!'' // false
!!0 // false
!!123 // true
!!null // false
!!{} // true
!![] // true
!!is a short and readable way to say explicitly: "convert this value to a boolean".
Boolean(value) produces exactly the same result and is often clearer in code other people read. !! stays popular because it is terse.
Use in logical expressions
! appears most often in condition checks:
const isLoggedIn = false;
if (!isLoggedIn) {
console.log('User is not authenticated');
}Here
!isLoggedInis!false, which istrue, so theifblock runs.
The same shape is convenient for checking missing data: if (!user), if (!items.length), if (!response.ok).
How it works internally
- JavaScript first converts the value to a logical type (
trueorfalse). - Then it inverts that result.
Example:
!'text' // step 1: 'text' becomes true; step 2: !true becomes falseThis is why ! never returns the original value: unlike && and ||, which return one of their operands, ! always hands back a new boolean.
Comparison with the other logical operators
| Operator | Name | Example | Result |
|---|---|---|---|
! | Logical NOT | !true | false |
&& | Logical AND | true && false | false |
|| | Logical OR | true || false | true |
A summary table by use case:
| Situation | What ! does | Example |
|---|---|---|
| Negates a logical value | Inverts a boolean | !true gives false |
Casts a value to boolean | Double !! | !!'js' gives true |
| Used in conditions | Checks for absence | if (!user), meaning "if user is not set" |
Common mistakes
- Assuming
![]or!{}givetrue. Arrays and objects are truthy, so the result isfalse; check emptiness withlengthorObject.keys(). - Mixing
!up with&&and||: those return one of the operands, while!always returns aboolean. - Forgetting that
!'0'isfalse: the string'0'is non-empty and therefore truthy, even though the number0is falsy. - Using
!!where the value is already boolean; double negation is only needed for the type cast. - Writing
if (!x)when you need to tell0or''apart fromnull/undefined. Preferif (x == null)orx ?? fallbackthere. - Confusing logical
!with!=and!==: they are different operators, and a space or an equals sign changes the meaning of the expression completely.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.