Skip to the content

Lesson 35 of 296

Logical operators: &&, || and !

What you will learn

  • AND, OR and NOT
  • Truth tables
  • Checking ranges and several conditions
  • Short-circuiting
  • Why && and || return values, not only booleans
  • Truthy and falsy values (first look)
On this page

The three logical operators

OperatorNameMeaning
&&ANDTrue only if both sides are true
||ORTrue if at least one side is true
!NOTFlips true to false and false to true

Truth tables

AND (&&)

ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

OR (||)

ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

NOT (!)

A!A
truefalse
falsetrue

Examples

const age = 25;
const hasTicket = true;

age >= 18 && hasTicket       true   (both are true)
age < 18 || hasTicket        true   (one is true)
!hasTicket                   false

Checking a range:

const score = 72;
score >= 50 && score <= 100     true

Do not write 50 <= score <= 100. JavaScript works it out as (50 <= score) <= 100, which becomes true <= 100, then 1 <= 100, which is always true.

Several conditions:

const canEdit = isOwner || isAdmin;
const canCheckout = isLoggedIn && basketCount > 0 && !isBlocked;

Order of operators

! runs first, then &&, then ||. So:

true || false && false

is true || (false && false), which is true.

Use brackets to make mixed conditions clear:

(isAdmin || isOwner) && !isLocked

Truthy and falsy (first look)

In conditions, JavaScript treats every value as either "truthy" (counts as true) or "falsy" (counts as false).

There are only 8 falsy values:

false   0   -0   0n   ""   null   undefined   NaN

Everything else is truthy, including some surprises:

"0"     "false"     " "     []     {}

An empty array and an empty object are truthy. Part 5 covers this in depth.

Short-circuiting

JavaScript stops checking as soon as it knows the answer.

  • With &&: if the left side is falsy, the answer is already false, so the right side is never run.
  • With ||: if the left side is truthy, the answer is already true, so the right side is never run.

This is used to avoid errors:

user && user.name

If user is null, JavaScript stops and does not try to read .name, so there is no error.

&& and || return a value, not just true or false

They return one of the two values you gave them:

  • a || b returns a if a is truthy, otherwise b.
  • a && b returns a if a is falsy, otherwise b.
"Sam" || "Guest"      "Sam"
"" || "Guest"         "Guest"
0 || 10               10
"Sam" && "Hi"         "Hi"
null && "Hi"          null

Default values with ||:

const displayName = userName || "Guest";

The trap: || also replaces values that are valid but falsy, such as 0 or "":

const quantity = 0;
const shown = quantity || 1;     1, but the real value was 0

The nullish coalescing operator ?? only replaces null and undefined:

const shown = quantity ?? 1;     0

Use ?? for default values when 0 or "" are valid. Part 5 covers it fully.

Double NOT

!!value turns any value into a real boolean:

!!"hello"     true
!!0           false
!!null        false

Common mistakes

  • Writing & or | (single). Those are different operators (bit operators, Part 10).
  • Writing a range as a < x < b.
  • Using || for defaults when 0 or "" are valid values.
  • Forgetting brackets when mixing && and ||.
  • Writing !x === 5 when you mean x !== 5. The ! runs first.

How to say it in an interview

"AND is true only if both sides are truthy, OR if at least one is, and NOT flips a value. Both AND and OR short-circuit and return one of their operands, not necessarily a boolean, which is why OR is used for defaults. But OR treats zero and empty string as missing, so for defaults where those are valid I use the nullish coalescing operator, which only falls back on null or undefined."

Key words

WordMeaning
Logical operator&&, || or !
Truth tableA table showing every possible input and result
TruthyA value that counts as true in a condition
FalsyOne of the 8 values that count as false
Short-circuitStopping early because the answer is already known
??Nullish coalescing: a default only for null or undefined

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.