How to Combine Conditions with AND (&&), OR (||) and NOT (!) in JavaScript
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
<a> && <b>
<a> || <b>
!<a>
| Part | What 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:

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
node main.js
true
false
true
Come in
false
guest

The command is the same in zsh, bash and PowerShell.
What Happened
age >= 18 && hasTicketistrue, because both sides are.age < 12 || age >= 65isfalse, because neither side is: 20 is neither under 12 nor 65 or over.!isBannedistrue, becauseisBannedisfalse.hasTicket && !isBannedlet the visitor in.!applies toisBannedbefore&&combines the two.user !== null && user.nameprintedfalseinstead of throwing an error. The left side wasfalse, so&&returned it without evaluatinguser.name. Reading.nameofnullwould have stopped the program.'' || 'guest'printedguest. An empty string counts as false, so||returned its right side. That's how||gives a default.
Common Mistakes
| Mistake | What happens | Do 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 false | Add parentheses: !(age >= 18) gives true |
Using || for a default when 0 or '' are valid values | 0 || 30 gives 30, replacing a real 0 | Use ??, 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 null | Use !! or Boolean(…) when you need a real true or false |
Checking a property of something that may be null without && first | TypeError: Cannot read properties of null | Check it first: user !== null && user.name |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A function on the right of && or || never runs | The left side already decided the result | Call it on its own line if it must always run |
A condition with ! gives the opposite of what you meant | ! binds tighter than comparisons | Put 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
- Logical AND (&&), Logical OR (||) and Logical NOT (!), on MDN.
