EN · USD

Language

Prices in

USDUSD · your currency EuroEUR · what you pay

You always pay in euros. Other currencies are a guide at today's rate.

Become a tutor Sign up
Try:
Search tips: combine words
python list
Articles with all the words
"for loop"
The exact words, in this order
python OR javascript
Either word
functions -arrow
Leave out articles with a word
title:booleans
Only in the title
code:append
Only in code examples
subject:python
In one subject
by:maximilian
By one writer
series:javascript
In one series
level:beginner
At one level
Try:
Search tips: combine words
python list
Articles with all the words
"for loop"
The exact words, in this order
python OR javascript
Either word
functions -arrow
Leave out articles with a word
title:booleans
Only in the title
code:append
Only in code examples
subject:python
In one subject
by:maximilian
By one writer
series:javascript
In one series
level:beginner
At one level

JavaScript Beginner English 7 min read

Logical operators: &&, || and !

Combine conditions with && (and), || (or) and ! (not), use brackets when you mix them, fill in default values with ||, and avoid the range mistake 10 < age < 20.

Series · Lesson 8 of 9 JavaScript, one lesson a day
In this article10
  1. The three operators
  2. && (AND): everything must be true
  3. || (OR): one is enough
  4. ! (NOT): flip it
  5. More than two conditions
  6. A sign-up form
  7. Shortcut 1: || for a default value
  8. Shortcut 2: JavaScript stops early
  9. Common mistakes
  10. Exercise

Real decisions rarely depend on one thing. "Enable the Sign up button if the name is filled in and the terms are ticked." "Show free shipping if the order is over 50 € or the customer is a member." The three logical operators &&, || and ! are how you write that. You've used them a little already. By the end of this lesson you'll know exactly how each one works, how to combine several without surprises, and two shortcuts frontend developers use every day.

The three operators

OperatorNameGives true when…
a && bANDboth a and b are true
a || bORat least one of them is true
!aNOTa is false (it flips the value)

&& (AND): everything must be true

console.log(true && true);   // true
console.log(true && false);  // false
console.log(false && true);  // false
console.log(false && false); // false

Only one result is true: when both sides are true. Think of a door with two locks: you need both keys.

const hasTicket = true;
const hasPassport = false;

if (hasTicket && hasPassport) {
  console.log("You can board the plane");
} else {
  console.log("Sorry, you can't board");
}

|| (OR): one is enough

console.log(true || true);   // true
console.log(true || false);  // true
console.log(false || true);  // true
console.log(false || false); // false

Only one result is false: when both sides are false. Think of a house with two doors: one open door is enough to get in.

const total = 35;
const isMember = true;

if (total >= 50 || isMember) {
  console.log("Free shipping!");
} else {
  console.log("Shipping: 5 €");
}

! (NOT): flip it

! turns true into false and false into true. It's great for checking that something is not the case:

const isLoggedIn = false;

if (!isLoggedIn) {
  console.log("Please sign in");
}

console.log(!true);  // false
console.log(!false); // true

More than two conditions

You can chain as many as you need. With only &&, all must be true. With only ||, one is enough:

const name = "Sara";
const age = 16;
const acceptedTerms = true;

const canSignUp = name !== "" && age >= 13 && acceptedTerms;
console.log("Can sign up:", canSignUp); // true

When you mix && and ||, use brackets, like in maths. They make the order clear for JavaScript and for anyone reading your code:

const isWeekend = true;
const isHoliday = false;
const isSunny = true;

// A day out: on a day off, and only if it's sunny
const dayOut = (isWeekend || isHoliday) && isSunny;
console.log("Day out:", dayOut); // true

Quick check

isAdmin is false, isOwner is true, isBlocked is true. What is (isAdmin || isOwner) && !isBlocked?

Why

(isAdmin || isOwner) is true, because the user is the owner. But !isBlocked is false, because the user is blocked. true && false is false.

A sign-up form

Here's how this looks on a real page. The button only works when all the rules are met. Press Run, then type a name and tick the box:

<!DOCTYPE html>
<html lang="en">
  <body style="padding: 16px; font-family: sans-serif">
    <input id="name" placeholder="Your name" />
    <label>
      <input id="terms" type="checkbox" /> I accept the terms
    </label>
    <p><button id="signup" disabled>Sign up</button></p>
    <script>
      const nameBox = document.getElementById("name");
      const terms = document.getElementById("terms");
      const button = document.getElementById("signup");

      function update() {
        const hasName = nameBox.value.trim() !== "";
        const canSignUp = hasName && terms.checked;

        button.disabled = !canSignUp;
        console.log("name:", hasName, "terms:", terms.checked);
      }

      nameBox.addEventListener("input", update);
      terms.addEventListener("change", update);
    </script>
  </body>
</html>

button.disabled = !canSignUp uses ! to flip the answer: when you can sign up, the button is not disabled.

Shortcut 1: || for a default value

|| doesn't only work with true and false. It gives back the first truthy value it finds. Developers use that to fill in a value when one is missing:

const typedName = "";
const name = typedName || "Guest";
console.log("Hello, " + name); // Hello, Guest

const typedName2 = "Omar";
const name2 = typedName2 || "Guest";
console.log("Hello, " + name2); // Hello, Omar

Read it as: use typedName, or if it's empty, use "Guest".

Shortcut 2: JavaScript stops early

&& and || stop as soon as they know the answer:

  • false && …: the answer is already false, so the right side is never checked.
  • true || …: the answer is already true, so the right side is never checked.

This lets you check that something exists before you use it:

const user = null; // nobody is signed in

if (user && user.name === "Sara") {
  console.log("Hi Sara");
} else {
  console.log("No user, or not Sara");
}

Without user &&, the code would try to read .name of null and crash. With it, JavaScript sees null (falsy), stops, and never touches user.name.

Common mistakes

Exercise

Show the answer
function canWatch(age, withParent) {
  return age >= 15 || (age >= 11 && withParent);
}

console.log(canWatch(16, false)); // true
console.log(canWatch(12, true));  // true
console.log(canWatch(12, false)); // false
console.log(canWatch(9, true));   // false

The brackets group the second rule: "11 or older and with a parent". Then || says either rule is enough. You don't need an if: the whole expression already is true or false, so you can return it directly.

More from Maximilian Bahrami