Skip to main content

How to Combine Conditions with AND (&&), OR (||) and NOT (!) in JavaScript

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

JavaScript combines conditions with three logical operators: a && b is true when both are true, a || b when at least one is, and !a turns true into false and false into true. && and || stop as soon as the left side decides the answer, which lets a condition check that something exists before it uses it.

Syntax​

Syntax
<a> && <b>
<a> || <b>
!<a>
PartWhat it does
<a> && <b>AND: true only when both <a> and <b> are true. If <a> is false, <b> isn't evaluated.
<a> || <b>OR: true when <a>, <b> or both are true. If <a> is true, <b> isn't evaluated.
!<a>NOT: true when <a> is false, and false when it's true.
<a>, <b>Any expressions, usually comparisons such as age >= 18 or true-or-false variables.

With values that aren't true or false, && and || give back one of their operands. MDN's logical AND reference: "More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy." ! always gives true or false.

The Example​

The example checks a visitor who is 20, has a ticket and isn't banned. It prints one &&, one || and one ! on their own, then combines && and ! in an if. The last two lines show what && and || return when their operands aren't true or false: a null user, and an empty string with a fallback.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which sets age to 20, hasTicket to true and isBanned to false, logs age at least 18 AND hasTicket, age under 12 OR at least 65, NOT isBanned, prints Come in when hasTicket AND NOT isBanned, then logs user not null AND user.name for a null user and an empty string OR guest.

main.js
const age = 20;
const hasTicket = true;
const isBanned = false;

console.log(age >= 18 && hasTicket);
console.log(age < 12 || age >= 65);
console.log(!isBanned);

if (hasTicket && !isBanned) {
console.log('Come in');
}

const user = null;
console.log(user !== null && user.name);
console.log('' || 'guest');

Step 2: Run It​

Terminal
node main.js
Output
true
false
true
Come in
false
guest

A zsh terminal where node main.js prints true, false, true, Come in, false and guest.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • age >= 18 && hasTicket is true, because both sides are.
  • age < 12 || age >= 65 is false, because neither side is: 20 is neither under 12 nor 65 or over.
  • !isBanned is true, because isBanned is false.
  • hasTicket && !isBanned let the visitor in. ! applies to isBanned before && combines the two.
  • user !== null && user.name printed false instead of throwing an error. The left side was false, so && returned it without evaluating user.name. Reading .name of null would have stopped the program.
  • '' || 'guest' printed guest. An empty string counts as false, so || returned its right side. That's how || gives a default.

Common Mistakes​

MistakeWhat happensDo this instead
Writing !age >= 18 to mean "not 18 or over"! applies to age first, so for an age of 15 it compares false >= 18 and gives falseAdd parentheses: !(age >= 18) gives true
Using || for a default when 0 or '' are valid values0 || 30 gives 30, replacing a real 0Use ??, which only replaces null and undefined: 0 ?? 30 gives 0
Expecting && or || to always give true or false'Ada' && 'Bob' gives 'Bob', and null && x gives nullUse !! or Boolean(…) when you need a real true or false
Checking a property of something that may be null without && firstTypeError: Cannot read properties of nullCheck it first: user !== null && user.name

Troubleshooting​

SymptomLikely causeFix
A function on the right of && or || never runsThe left side already decided the resultCall it on its own line if it must always run
A condition with ! gives the opposite of what you meant! binds tighter than comparisonsPut the comparison in parentheses after !

FAQ​

What's the difference between || and ?? in JavaScript?​

|| falls back when its left side is any falsy value, such as false, 0, '', null, undefined or NaN. ?? falls back only on null and undefined, so it keeps a 0 or an empty string that was set on purpose.

What does !! do?​

It applies NOT twice, turning any value into true or false: !!'Ada' is true, and !!'' is false.

Which runs first, && or ||?​

&& binds tighter than ||, so a || b && c means a || (b && c): true || false && false is true, while (true || false) && false is false. Parentheses make a mixed condition easier to read either way.

Conclusion​

&& needs both sides, || needs one, and ! flips a condition. Because && and || stop as soon as the answer is known, the left side can guard the right one, as in user !== null && user.name.

Additional Resources​