Comparison Operators

Learn how to compare values in JavaScript using comparison operators to make decisions in your code.

What is a Comparison Operator?Link to this section

A comparison operator is a symbol used to compare two values and return a boolean result of either true or false. These operators form the foundation of decision making in your programs.

This example compares the score variable with 50 and stores the boolean result.

  • score is the **left operand** representing the value being checked
  • is the **comparison operator** meaning greater than
  • 50 is the **right operand** representing the threshold value
  • true is the **boolean output** indicating the condition is met

Comparison operators are used everywhere in programming to check conditions, validate inputs, and control the flow of execution.

Why Do We Need Comparison Operators?Link to this section

We need comparison operators because programs constantly need to make choices based on changing data.

  • Check if a student passing mark is achieved
  • Verify if a user entered the correct login password
  • Determine if a customer qualifies for a free shipping discount
  • Check if an item in stock is running low
  • Compare user age to website age restrictions
  • Evaluate if a shopping cart total exceeds a budget limit

Checks if the user age meets the legal voting requirement.

Without comparison operators, our code would not be able to adapt to different inputs or make logical decisions.

About Comparison OperatorsLink to this section

Comparison operators evaluate two values and always return a boolean value. They are grouped into equality checks and relational checks. JavaScript also provides strict comparison operators that check both value and data type.

CategoryPurposeExample Operators
RelationalCompare relative size>, <, >=, <=
EqualityCompare equality==, ===, !=, !==

Types of JavaScript Comparison OperatorsLink to this section

1. Greater Than Operator (>)Link to this section

The greater than operator checks if the value on the left is strictly greater than the value on the right. If it is, it returns true; otherwise, it returns false.

Checks if the price is greater than 100.

note

The greater than operator works with both numbers and strings based on alphabetical order.

2. Less Than Operator (<)Link to this section

The less than operator checks if the value on the left is smaller than the value on the right. It returns true only if the left side is smaller.

Checks if the temperature is below 20 degrees.

tip

Use the less than operator to check if stock levels or counts drop below a safe minimum.

3. Greater Than or Equal To Operator (>=)Link to this section

This operator checks if the left value is greater than or equal to the right value. It returns true if either condition is satisfied.

A student with exactly 50 marks passes the exam.

warning

Be careful with the order of symbols. It must be >= and never =>.

4. Less Than or Equal To Operator (<=)Link to this section

This operator checks if the left value is less than or equal to the right value. It returns true if the left value is smaller or identical.

Checks if the cart item count is within the limit.

note

The symbol is written as <= with the less-than sign first followed by the equals sign.

5. Equal To Operator (==)Link to this section

The equal to operator checks if two values are equal. Crucially, it performs type coercion, meaning it converts different types to match before comparing.

JavaScript converts the string '5' to the number 5 before comparing.

warning

Type coercion can lead to unexpected bugs. Avoid using == unless you specifically want type conversion.

6. Strict Equal To Operator (===)Link to this section

The strict equal to operator checks if two values are equal and share the exact same data type. It does not perform type conversion.

Returns false because a number and a string are not the same type.

tip

Always prefer using strict equality (===) over loose equality (==) in modern JavaScript.

7. Not Equal To Operator (!=)Link to this section

The not equal to operator checks if two values are not equal. Like the loose equality operator, it performs type coercion.

Returns true because 5 is not equal to 10.

warning

Avoid using != for the same reasons you should avoid ==.

8. Strict Not Equal To Operator (!==)Link to this section

The strict not equal to operator checks if two values are not equal or not of the same type. It is the strict counterpart to !=.

Returns true because even though values match, their types are different.

tip

Always use strict inequality (!==) when ensuring two values do not match.

Comparing StringsLink to this section

JavaScript also allows you to compare strings alphabetically. Uppercase letters generally come before lowercase letters in ASCII order.

Compares strings alphabetically.

note

String comparisons check character codes sequentially from left to right.

Complete Coding ExampleLink to this section

A complete program verifying a bank withdrawal using comparison operators.

This script combines relational and strict equality operators to check bank account balances.

Important NotesLink to this section

  • Comparison operators always evaluate to a boolean value: true or false.
  • The greater than symbol is > and less than is <.
  • Greater than or equal to is written as >=.
  • Less than or equal to is written as <=.
  • Loose equality (==) converts data types before comparing.
  • Strict equality (===) checks both value and data type without conversion.
  • Always prefer strict equality (===) and strict inequality (!==) in your code.
  • Strings can be compared alphabetically using standard comparison operators.
  • Order of characters matters when comparing strings.
  • Comparison results can be stored in variables for later use in decisions.

SummaryLink to this section

In this lesson, you learned how JavaScript comparison operators evaluate values to return true or false. We covered relational operators like greater than and less than, as well as equality operators including strict checks. These operators form the foundation of conditional logic, enabling your programs to make smart decisions.

Check Your UnderstandingLink to this section

Javascript Comparison Operators quiz
Question 1 of 6

What does the expression `10 > 5` evaluate to?

Practice ChallengesLink to this section

1. Passing Grade Checker

easy

Create a variable named studentScore with a value of 72. Create another variable named hasPassed that uses a comparison operator to check if studentScore is greater than or equal to 50. Print hasPassed to the console.

Expected output

true

Hint: Use the >= operator.

2. Speed Limit Warning

easy

Create a variable carSpeed with a value of 85 and speedLimit with a value of 60. Create a variable isSpeeding that checks if carSpeed is greater than speedLimit. Print the result.

Expected output

true

Hint: Use the > operator to compare carSpeed and speedLimit.

3. Strict Password Match

medium

Create two variables storedPassword as 'secret123' and enteredInput as 12345. Create a variable isMatch using strict inequality (!==) to check if they are not equal. Print isMatch.

Expected output

true

Hint: Use the !== operator.

4. Discount Eligibility

medium

Create a variable cartTotal with a value of 120 and minimumForDiscount with a value of 100. Create a variable getsDiscount checking if cartTotal is greater than or equal to minimumForDiscount. Print getsDiscount.

Expected output

true

Hint: Compare the totals using >=.

5. Age Verification System

hard

Create a variable userAge set to 20 and minimumAge set to 18. Create a boolean variable isAllowed checking if userAge is greater than or equal to minimumAge, then print the result.

Expected output

true

Hint: Use the >= operator to check age requirements.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion