Skip to main content

The ! operator

The ! operator is called logical negation (NOT). It inverts (flips) the logical value of an expression.


Rule:

javascript
!truefalse !falsetrue

If a value is truthy - it becomes false. If it is falsy - it becomes true.


Examples of using !

javascript
!true // false !false // true !0 // true (0 - a falsy value) !1 // false (1 - truthy) !"" // true (an empty string - falsy) !"hello" // false (a non-empty string - truthy) !undefined // true !null // true !NaN // true ![] // false (an array - truthy) !{} // false (an object - truthy)

Double negation !!

A double operator is very common in JavaScript:

javascript
!!value

It is used to coerce any value to the boolean type.

Example:

javascript
!!'hello' // true !!'' // false !!0 // false !!123 // true !!null // false !!{} // true !![] // true

!! is a short and clear way to explicitly say: "Convert this value to boolean".


In logical expressions

! is often used to check conditions:

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

Here, !isLoggedIn!falsetrue, so the if block will execute.


How it works internally

  1. JS first coerces the value to a logical type (true / false);
  2. then it inverts it.

Example:

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

Comparison with other logical operators

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

Summary

SituationWhat ! doesExample
Negates a logical value!true → false
Coerces a value to boolean!!'js' → true
Used in conditionsif (!user) → "if user is not set"

Short Answer

Interview ready
Premium

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