Logical Operators

Learn how to combine multiple conditions in JavaScript using AND, OR, and NOT logical operators.

What is a Logical Operator?Link to this section

A logical operator is a special symbol used to combine two or more comparisons so your program can make decisions based on multiple rules at once. They return a boolean value of either true or false depending on the outcome of the combined checks.

This example checks if both conditions are met before allowing entry.

  • `hasId` and `hasTicket` are the comparison variables holding booleans
  • `&&` is the logical **AND operator** combining the two conditions
  • `canEnter` stores the final boolean result of the combined expression

Logical operators are used everywhere in web development to control access, validate forms, and toggle UI states.

Why Do We Need Logical Operators?Link to this section

Without logical operators, checking multiple conditions requires messy nested if-statements that are hard to read and maintain.

  • Verify if a user has both a valid email and a correct password to log into an account
  • Check if a customer is either a club member or has a discount coupon to get free shipping
  • Ensure a student passed both the math exam and the science exam to graduate
  • Confirm that a form input is not empty and has at least eight characters
  • Validate if a theater visitor is older than 18 or accompanied by an adult
  • Check if a bank account is active and has enough balance before allowing a withdrawal

Checking two rules at once prevents underage driving unless permission is explicitly granted.

The code evaluates two separate comparisons and combines them so the final decision happens in a single readable line.

About Logical OperatorsLink to this section

Logical operators work by evaluating truthy and falsy values, not just strict booleans. JavaScript evaluates them from left to right and stops as soon as the final result is known, a behavior known as short-circuit evaluation. They are grouped into three main symbols representing conjunction, disjunction, and negation.

OperatorNameSymbolExample
Logical ANDReturns true if both sides are true&&true && false
Logical ORReturns true if at least one side is true||true || false
Logical NOTReverses the boolean value!!true

The OR operator returns true because at least one condition is true.

Types of JavaScript Logical OperatorsLink to this section

1. Logical AND Operator (&&)Link to this section

The logical AND operator takes two expressions and evaluates to true only if both expressions are true. If even one side is false, the entire expression evaluates to false.

Both conditions are met, so the student passes.

Checking if a customer qualifies for a VIP discount.

Short-circuiting prevents dividing by zero because the first part is false.

tip

Place the most likely false condition first when using logical AND to make your code run faster through short-circuiting.

warning

Do not confuse the logical AND operator (&&) with the bitwise AND operator (&).

2. Logical OR Operator (||)Link to this section

The logical OR operator takes two expressions and evaluates to true if at least one of the expressions is true. It only returns false if both sides are false.

Checkout is allowed because the customer has a gift card.

Determining if you can sleep in based on the day.

note

Logical OR returns the value of the first truthy operand it encounters, or the last operand if all are falsy.

3. Logical NOT Operator (!)Link to this section

The logical NOT operator is represented by an exclamation mark. It takes a single value and flips its boolean state from true to false, or from false to true.

Flips false to true.

Checking if a user is allowed to post content.

A non-empty string is truthy, so NOT makes it false.

tip

You can use double negation (!!) to quickly convert any non-boolean value into its proper boolean equivalent.

Short-Circuit EvaluationLink to this section

JavaScript logical operators stop evaluating as soon as the outcome is certain. For AND, if the first operand is false, the second is ignored. For OR, if the first operand is true, the second is ignored.

Prevents runtime errors by stopping before accessing a property of null.

note

Short-circuiting is a common pattern for guarding against null or undefined property access.

Nullish Coalescing vs Logical ORLink to this section

While logical OR (||) falls back on any falsy value like 0 or empty string, the nullish coalescing operator (??) only falls back when the value is strictly null or undefined.

OR treats 0 as falsy and replaces it, while nullish coalescing keeps 0.

warning

Using || with numeric settings like 0 or false can cause bugs if those numbers are valid inputs.

Complete Coding ExampleLink to this section

A complete store checkout check using multiple logical operators.

This program combines comparison and logical operators to determine purchase permissions and shipping eligibility in an online store.

Important NotesLink to this section

  • Logical operators work with booleans but can also evaluate non-boolean truthy and falsy values.
  • The logical AND (&&) operator returns true only when both operands are true.
  • The logical OR (||) operator returns true if at least one operand is true.
  • The logical NOT (!) operator reverses a boolean value.
  • Short-circuit evaluation stops checking operands once the final boolean outcome is certain.
  • Logical AND has higher operator precedence than logical OR.
  • Use parentheses to explicitly control the order of evaluation in complex conditions.
  • Double negation (!!) converts any JavaScript value into a pure boolean.
  • Nullish coalescing (??) is safer than logical OR when dealing with numbers like zero.
  • Falsy values in JavaScript include false, 0, empty strings, null, undefined, and NaN.

SummaryLink to this section

Logical operators are essential tools for building complex conditions in JavaScript programs. By combining comparisons with AND, OR, and NOT, you can control program flow, validate inputs, and make smart decisions. Mastering short-circuit evaluation will make your code cleaner and more efficient.

Check Your UnderstandingLink to this section

Javascript Logical Operators quiz
Question 1 of 6

What does the expression `true && false` evaluate to?

Practice ChallengesLink to this section

1. Movie Ticket Discount

easy

Create two variables, isStudent and isSenior, both set to boolean values. Write an expression using logical OR to check if the person qualifies for a discount and print the result.

Expected output

true

Hint: Use the || operator between the two variables.

2. Login Validator

easy

Declare isLoggedIn and hasAdminRights. Write a check using logical AND to see if the user can access the dashboard and print the result.

Expected output

false

Hint: Use the && operator to require both conditions to be true.

3. Free Shipping Eligibility

medium

A customer gets free shipping if their cart total is at least 50 or they are a premium member. Declare cartTotal = 60 and isPremium = false. Print whether they get free shipping.

Expected output

true

Hint: Combine a comparison and a boolean variable with ||.

4. Form Validation Check

medium

Ensure a username is entered and its length is greater than 3. Declare username = "alex". Write a check using logical AND and print the result.

Expected output

true

Hint: Check if username is truthy and username.length > 3.

5. Complex Access Control

hard

A roller coaster ride allows entry if the rider is taller than 120cm OR has a VIP pass, AND is not banned. Declare height = 130, hasVip = false, and isBanned = false. Print the final access result.

Expected output

true

Hint: Group the OR condition in parentheses before applying AND with the ban check.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion