Skip to main content

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

  • !true is false, !false is true.
  • If the value is truthy, the result is false; if it is falsy, the result is true.
  • 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 give false.
  • !!value is the idiom for explicitly casting any value to boolean.
  • The result of ! is always of type boolean, which makes the operator handy in checks like if (!user).

Quick example

javascript
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:

javascript
!true // false !false // true

If the value is truthy, it becomes false. If the value is falsy, it becomes true.

A full set of examples for values of different types:

javascript
!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:

javascript
!!value

It 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.

javascript
!!'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:

javascript
const isLoggedIn = false; if (!isLoggedIn) { console.log('User is not authenticated'); }

Here !isLoggedIn is !false, which is true, so the if block runs.

The same shape is convenient for checking missing data: if (!user), if (!items.length), if (!response.ok).

How it works internally

  1. JavaScript first converts the value to a logical type (true or false).
  2. Then it inverts that result.

Example:

javascript
!'text' // step 1: 'text' becomes true; step 2: !true becomes false

This 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

OperatorNameExampleResult
!Logical NOT!truefalse
&&Logical ANDtrue && falsefalse
||Logical ORtrue || falsetrue

A summary table by use case:

SituationWhat ! doesExample
Negates a logical valueInverts a boolean!true gives false
Casts a value to booleanDouble !!!!'js' gives true
Used in conditionsChecks for absenceif (!user), meaning "if user is not set"

Common mistakes

  • Assuming ![] or !{} give true. Arrays and objects are truthy, so the result is false; check emptiness with length or Object.keys().
  • Mixing ! up with && and ||: those return one of the operands, while ! always returns a boolean.
  • Forgetting that !'0' is false: the string '0' is non-empty and therefore truthy, even though the number 0 is falsy.
  • Using !! where the value is already boolean; double negation is only needed for the type cast.
  • Writing if (!x) when you need to tell 0 or '' apart from null/undefined. Prefer if (x == null) or x ?? fallback there.
  • 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.