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
| Operator | Name | Meaning |
|---|---|---|
&& | AND | True only if both sides are true |
|| | OR | True if at least one side is true |
! | NOT | Flips true to false and false to true |
Truth tables
AND (&&)
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
OR (||)
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
NOT (!)
| A | !A |
|---|---|
| true | false |
| false | true |
Examples
const age = 25;
const hasTicket = true;
age >= 18 && hasTicket true (both are true)
age < 18 || hasTicket true (one is true)
!hasTicket falseChecking a range:
const score = 72;
score >= 50 && score <= 100 trueDo 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 && falseis true || (false && false), which is true.
Use brackets to make mixed conditions clear:
(isAdmin || isOwner) && !isLockedTruthy 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 NaNEverything 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.nameIf 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 || breturnsaifais truthy, otherwiseb.a && breturnsaifais falsy, otherwiseb.
"Sam" || "Guest" "Sam"
"" || "Guest" "Guest"
0 || 10 10
"Sam" && "Hi" "Hi"
null && "Hi" nullDefault 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 0The nullish coalescing operator ?? only replaces null and undefined:
const shown = quantity ?? 1; 0Use ?? 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 falseCommon mistakes
- Writing
&or|(single). Those are different operators (bit operators, Part 10). - Writing a range as
a < x < b. - Using
||for defaults when0or""are valid values. - Forgetting brackets when mixing
&&and||. - Writing
!x === 5when you meanx !== 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
| Word | Meaning |
|---|---|
| Logical operator | &&, || or ! |
| Truth table | A table showing every possible input and result |
| Truthy | A value that counts as true in a condition |
| Falsy | One of the 8 values that count as false |
| Short-circuit | Stopping 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.