thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

06.05 · Logical & comparison operators / Learn · 4 min

Combine Boolean conditions

Does the workshop need both things, either thing, or the opposite state? Read the rule before choosing an operator.

Activity 5 of 6 in Logical & comparison operators

All courses

JavaScript Basics / 06.05

Combine Boolean conditions

Not started

Choose AND for both, OR for at least one, and NOT for an opposite Boolean state.

More options
Rob inspects two switches and a lamp, explores alternative paths to a door, and studies a negation prop.
Rob’s comic strip · Logical & comparison operators

Both versus at least one

With Boolean operands, && is true only when both operands are true. || is true when at least one operand is true, including when both are true.

Use parentheses to make a combined rule readable. Although && has higher precedence than ||, explicit grouping helps the next developer understand what belongs together.

Separate the entry rule from the equipment rule
example.jsjavascript · example
const hasTicket = true;
const isRegistered = false;
const hasLaptop = true;
const canEnter = hasTicket || isRegistered;
const canJoinCoding = canEnter && hasLaptop;
console.log(canEnter); // true
console.log(canJoinCoding); // true
console.log(!hasLaptop); // false

The result is not always a Boolean

&& and || evaluate from left to right and can stop as soon as the result is determined. They return one of their operand values; they do not always return true or false.

Keep this unit’s rules based on actual Booleans or comparisons. ! always returns a Boolean opposite of the operand’s truthiness. An empty string is falsy, while a nonempty string such as "false" is truthy.

Recognize a returned operand
example.jsjavascript · example
console.log("" || "Guest"); // Guest
console.log("Ready" && "Go"); // Go
console.log(!"false"); // false

Combine conditions for a workshop

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const hasTicket = true;
const isRegistered = false;
const hasLaptop = true;
const canEnter = hasTicket || isRegistered;
const canJoinCoding = canEnter && hasLaptop;
console.log(canEnter); // true
console.log(canJoinCoding); // true
console.log(!hasLaptop); // false

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Say the rule aloud: both, at least one, or the opposite.