Ad space (banner)
🟨JavaScript Lessons
Lesson 19 / 69

Logical Operators (AND, OR, NOT)

In this lesson you'll master JavaScript's logical operators (AND, OR, NOT) so you can make decisions that combine multiple conditions. This is for people searching "JavaScript logical operators" or "JavaScript && || usage."

When you want to combine multiple conditions, you use logical operators. && (AND) means "both must be true," || (OR) means "at least one is true," and ! (NOT) means "flip it (negate it)." These are essential for judging combined conditions like "18 or older, and has a ticket." They're extremely common operators, used constantly inside if statement conditions.

The sample code checks "18 or older and has a ticket" with age >= 18 && hasTicket, and checks "under 18, or doesn't have a ticket" with age < 18 || !hasTicket. Using parentheses to make precedence explicit helps prevent misreading even complex conditions. JavaScript also has a property called short-circuit evaluation: "if the result is already decided by the left side alone, the right side isn't evaluated at all."

A common beginner stumbling block is confusing the meanings of && and ||. It helps to put the condition into words in plain English first — "both must be satisfied" or "just one is enough" — before translating it into code, which reduces mistakes. Forgetting to add ! and ending up with the reverse of the intended condition is another common error, so check carefully.

In real development, logical operators show up constantly in access-control logic — for example, showing a certain feature only when a user is logged in *and* has admin privileges. Being able to correctly assemble logical operators also matters a lot in real-world validation logic where multiple conditions are intertwined.

Logical operators can also be used with non-boolean values. Using || to write "use a default value if nothing was set" (const name = input || "Guest";) is a well-established, classic technique that predates the nullish coalescing operator.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)