&&
Returns the first falsy value, or the last value if all are truthy.
Logical operators combine conditions, invert values and short-circuit expressions. They return values, not always booleans.
const canDrive = age >= 18 && hasLicense; const name = inputName || "Anonymous"; const isClosed = !isOpen;
Operators & Expressions
The main logical operators are &&, || and !. They are used in conditions, guards, defaults and compact rendering logic.
A key JavaScript detail: && and || return one of the original operand values. They do not automatically return true or false unless the operands are booleans.
Short-circuiting means JavaScript may skip the right-hand expression. This is useful for guards, but it also means side effects on the right may never run.
Returns the first falsy value, or the last value if all are truthy.
Returns the first truthy value, or the last value if none are truthy.
Converts a value to boolean and flips it.
The right side only runs when the operator needs it.
Examples
const hasValidAge = age >= 16.5; const hasSelectedOption = optionName !== ""; const canSubmit = hasValidAge && hasSelectedOption; button.disabled = !canSubmit;
const discount = 0; const visibleDiscount = discount || "No discount"; console.log(visibleDiscount); // "No discount"
Rules that matter
JavaScript operators are compact, but compact is not always clear. Convert values before comparing or calculating, use strict checks by default and add parentheses when a future reader would otherwise need to remember a precedence table.
hasPaid && canStart reads better than a pile of raw comparisons.
&& and || can return strings, numbers, objects or booleans.
|| treats 0, false and empty string as fallback triggers.
Do not rely on skipped right-hand expressions to perform important work.
!isOpen is better than comparing isOpen === false in many simple cases.
When && and || appear together, parentheses make intent visible.
Production thinking
Logical operators decide whether actions are allowed, forms can submit and UI pieces appear. They deserve clear names.
Logical state often controls disabled controls, aria-expanded, error visibility and focus flow.
Production logic should be readable enough to review. Compact boolean cleverness is not worth hidden bugs.
Conditional rendering can hide links or content. If logic is wrong, crawlers and users may miss important page sections.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Say the answer in plain language. If you can explain the operator behavior, you can debug the expression.