JavaScript Comparison Operators: Compare Values Correctly

JavaScript comparison operators compare two values and return either true or false.

A shopping website can check whether a price is above a limit. A login form can check whether a value matches another value. A stock system can check whether quantity is greater than zero.

Comparison operators are essential because JavaScript conditions depend on these true-or-false results.

Course Home: JavaScript Tutorial for Beginners
Previous Lesson: JavaScript Operators Explained
Next Lesson: JavaScript Logical Operators Explained

Quick Answer

The main JavaScript comparison operators are:

OperatorMeaningExampleResult
===Strictly equal5 === 5true
!==Strictly not equal5 !== 3true
>Greater than10 > 5true
<Less than3 < 8true
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 6true

JavaScript also has == and !=, but they can convert values before comparing them.

For beginner and modern JavaScript code, prefer:

===
!==

when you want predictable comparisons.

What Are Comparison Operators in JavaScript?

Comparison operators compare values.

For example:

const age = 20;

console.log(age >= 18);

Output:

true

JavaScript compares 20 with 18.

Because 20 is greater than or equal to 18, the result is true.

Another example:

const price = 500;

console.log(price < 300);

Output:

false

The comparison itself produces a boolean value.

If you need a refresher on booleans, see the JavaScript Data Types tutorial.

Why Comparison Operators Matter

Websites constantly need to make checks.

For example:

const stock = 5;

console.log(stock > 0);

Output:

true

A shopping website can use this result to decide whether a product is available.

Other common checks include:

  • Is the user old enough?
  • Is the password long enough?
  • Is the cart total above free-shipping value?
  • Is stock greater than zero?
  • Does the entered value match the expected value?
  • Is one date or number larger than another?

Comparison operators provide the true or false values that later work with JavaScript if else conditions and logical operators.

Strict Equality Operator ===

The strict equality operator checks two things:

  1. Are the values equal?
  2. Are the data types equal?

Example:

console.log(5 === 5);

Output:

true

Both values are numbers and both contain 5.

Now compare:

console.log(5 === "5");

Output:

false

The values may look similar, but their types are different.

The first value is a number:

5

The second is a string:

"5"

Strict equality does not convert one type into another before comparing them.

Real Website Example: Checking a User Role

Suppose a website stores:

const userRole = "admin";

You can check it with:

console.log(userRole === "admin");

Output:

true

This comparison is clear because both the value and type must match.

Strict Inequality Operator !==

The strict inequality operator checks whether two values are different in value or type.

Example:

console.log(5 !== 3);

Output:

true

The values are different.

Now:

console.log(5 !== 5);

Output:

false

The values and types match.

This also returns true:

console.log(5 !== "5");

Output:

true

The visible values look similar, but one is a number and the other is a string.

== vs === in JavaScript

JavaScript also has the loose equality operator:

==

It may convert values before comparing them.

For example:

console.log(5 == "5");

Output:

true

JavaScript converts the values during the loose comparison.

Now compare the same values with strict equality:

console.log(5 === "5");

Output:

false

The types are different.

Which One Should Beginners Use?

For most modern JavaScript code, prefer:

===

instead of:

==

Strict equality is easier to reason about because it does not perform loose type conversion before the comparison.

!= vs !== in JavaScript

The loose inequality operator is:

!=

For example:

console.log(5 != "5");

Output:

false

The loose comparison treats the values as equal after conversion.

Strict inequality behaves differently:

console.log(5 !== "5");

Output:

true

For predictable beginner code, prefer:

!==

Greater Than Operator >

The greater than operator checks whether the left value is larger than the right value.

console.log(10 > 5);

Output:

true

Another example:

console.log(3 > 8);

Output:

false

Real Website Example: Free Shipping

Suppose free shipping starts above ₹1,000.

const cartTotal = 1500;

console.log(cartTotal > 1000);

Output:

true

If exactly ₹1,000 should also qualify, you need >= instead.

Less Than Operator <

The less than operator checks whether the left value is smaller.

console.log(3 < 10);

Output:

true

Another example:

console.log(12 < 5);

Output:

false

Real Website Example: Low Stock

const stock = 3;

console.log(stock < 5);

Output:

true

A website could use this check when deciding whether to show a low-stock message.

Greater Than or Equal Operator >=

The >= operator returns true when the left value is either:

  • Greater than the right value
  • Equal to the right value

Example:

console.log(10 >= 5);

Output:

true

This also returns true:

console.log(10 >= 10);

Output:

true

Real Website Example: Minimum Age

const age = 18;

console.log(age >= 18);

Output:

true

The value meets the minimum.

Less Than or Equal Operator <=

The <= operator returns true when the left value is:

  • Less than the right value
  • Equal to the right value

Example:

console.log(5 <= 10);

Output:

true

This also returns true:

console.log(10 <= 10);

Output:

true

Real Website Example: Quantity Limit

Suppose a customer can order up to five items.

const quantity = 5;

console.log(quantity <= 5);

Output:

true

If the quantity becomes six:

const quantity = 6;

console.log(quantity <= 5);

Output:

false

Comparison Operators Return Booleans

Every comparison produces a boolean result.

For example:

const result = 10 > 5;

console.log(result);

Output:

true

Check its type:

console.log(typeof result);

Output:

boolean

This is why comparisons work naturally inside conditions.

For example:

const age = 20;

if (age >= 18) {
    console.log("Access allowed");
}

You will learn this structure properly in the JavaScript if else tutorial.

Comparing Numbers

Number comparisons are usually straightforward.

console.log(20 > 10);
console.log(5 < 9);
console.log(7 >= 7);
console.log(4 <= 3);

Output:

true
true
true
false

Be careful when a number comes from a form as text.

This:

const age = "18";

stores a string, not a number.

Check:

console.log(typeof age);

Output:

string

You will learn how to convert values in the JavaScript Type Conversion lesson.

Comparing Strings

JavaScript can also compare strings.

For example:

console.log("apple" === "apple");

Output:

true

Different capitalization does not match:

console.log("Apple" === "apple");

Output:

false

JavaScript string comparisons are case-sensitive.

Greater Than and Less Than With Strings

You may also see comparisons such as:

console.log("banana" > "apple");

Output:

true

JavaScript compares strings based on their character values.

However, string ordering can become confusing with uppercase letters, lowercase letters, languages, and user-facing sorting.

For normal website sorting, do not build advanced alphabetical sorting logic from simple > and < comparisons alone.

You will learn better string and array sorting methods later.

Comparing Booleans

Booleans can be compared with strict equality.

const loggedIn = true;

console.log(loggedIn === true);

Output:

true

However, when a boolean is already available, you often do not need to write:

if (loggedIn === true) {
    console.log("Welcome");
}

You can usually write:

if (loggedIn) {
    console.log("Welcome");
}

The shorter version is clear when loggedIn is already a boolean.

Comparing null and undefined

Strict equality treats null and undefined as different values.

console.log(null === undefined);

Output:

false

Loose equality behaves differently:

console.log(null == undefined);

Output:

true

This is another example of why === gives beginners more predictable behavior.

If these values are unfamiliar, review JavaScript null and undefined in the Data Types lesson.

Comparing Objects

This is an important JavaScript behavior.

Consider:

const product1 = {
    name: "Laptop"
};

const product2 = {
    name: "Laptop"
};

console.log(product1 === product2);

Output:

false

The objects contain similar information, but they are two different objects.

Now:

const product1 = {
    name: "Laptop"
};

const product2 = product1;

console.log(product1 === product2);

Output:

true

Both variables refer to the same object.

You will study object references more deeply in the JavaScript Objects tutorial.

Comparing Arrays

Arrays behave similarly to objects.

const items1 = [1, 2, 3];
const items2 = [1, 2, 3];

console.log(items1 === items2);

Output:

false

They contain the same numbers, but they are separate arrays.

This returns true:

const items1 = [1, 2, 3];
const items2 = items1;

console.log(items1 === items2);

Output:

true

You will learn more in the JavaScript Arrays tutorial.

Comparing NaN

NaN means Not-a-Number.

You may encounter it after an invalid numeric conversion or calculation.

A surprising JavaScript rule is:

console.log(NaN === NaN);

Output:

false

To check specifically for NaN, use:

console.log(Number.isNaN(NaN));

Output:

true

You do not need to use NaN often yet, but this behavior is useful to recognize when debugging calculations.

Real-World Example: Checking Stock

Suppose:

const stock = 8;

Check whether the product is available:

const available = stock > 0;

console.log(available);

Output:

true

You could later use:

if (stock > 0) {
    console.log("In stock");
}

The comparison:

stock > 0

produces the boolean used by the condition.

Real-World Example: Free Shipping Check

Suppose free shipping starts at ₹1,000.

const cartTotal = 1200;
const freeShippingMinimum = 1000;

const getsFreeShipping = cartTotal >= freeShippingMinimum;

console.log(getsFreeShipping);

Output:

true

If the cart total were:

const cartTotal = 900;

the comparison would return:

false

Real-World Example: Password Length

Suppose a password needs at least eight characters.

const password = "mypassword";

const validLength = password.length >= 8;

console.log(validLength);

Output:

true

The comparison checks a number:

password.length

against:

8

Later, you can combine several checks with JavaScript logical operators.

Real-World Example: Product Price Range

Suppose:

const price = 750;

You can check whether the price is above ₹500:

console.log(price > 500);

Output:

true

You can also check whether it is at most ₹1,000:

console.log(price <= 1000);

Output:

true

To require both conditions at the same time, you will use the logical AND operator in the next lesson.

Real-World Example: Checking a Selected Option

Imagine a form stores:

const paymentMethod = "card";

You can check:

console.log(paymentMethod === "card");

Output:

true

This type of comparison is common in forms, filters, tabs, dropdowns, and user settings.

Strict Equality With Different Data Types

Compare:

const quantity = 5;
const formValue = "5";

console.log(quantity === formValue);

Output:

false

This is useful because it reveals that the values have different types.

Check them:

console.log(typeof quantity);
console.log(typeof formValue);

Output:

number
string

When your data should be numeric, convert it intentionally instead of relying on loose equality.

Common Beginner Mistakes

Using = Instead of ===

This:

let age = 18;

assigns a value.

This:

age === 18

compares a value.

Do not confuse assignment with comparison.

Review the JavaScript Operators lesson if needed.

Using == Without Understanding Type Conversion

console.log(5 == "5");

returns:

true

This can hide a type mismatch.

Prefer:

console.log(5 === "5");

which returns:

false

Forgetting That Strings Are Case-Sensitive

console.log("Admin" === "admin");

Output:

false

The capitalization is different.

Comparing Two Separate Objects Directly

console.log({ name: "Amit" } === { name: "Amit" });

Output:

false

The two objects are separate references.

Comparing Arrays by Their Contents With ===

console.log([1, 2] === [1, 2]);

Output:

false

Strict equality does not compare array contents item by item.

Forgetting the Equal Part of >= or <=

This:

age > 18

does not include age 18.

If 18 should qualify, use:

age >= 18

Comparing Form Values Without Checking Their Type

Many HTML form values arrive as strings.

A value that looks like:

10

may actually be:

"10"

Check or convert the type before important numeric comparisons.

Best Practices for JavaScript Comparisons

Prefer strict equality:

===

and strict inequality:

!==

for most beginner and modern JavaScript code.

Use descriptive boolean names:

const hasStock = stock > 0;
const isAdult = age >= 18;
const isCorrectRole = role === "admin";

Keep comparisons easy to read.

Prefer:

const qualifiesForFreeShipping = cartTotal >= 1000;

instead of burying the same check inside a long expression.

Check data types when values come from forms, APIs, URLs, or local storage.

Use parentheses when combining more complex conditions later with logical operators.

Beginner Exercise

Create:

const age = 20;
const minimumAge = 18;
const userName = "Amit";

Now predict the result of each comparison:

console.log(age > minimumAge);
console.log(age < minimumAge);
console.log(age >= 20);
console.log(age === "20");
console.log(userName === "Amit");
console.log(userName !== "Riya");

Run the code after making your predictions.

Then explain why:

age === "20"

returns false.

Challenge Exercise

Create:

const productPrice = 1200;
const freeShippingMinimum = 1000;
const stock = 4;

Create boolean variables for these questions:

  1. Is the product price at least ₹1,000?
  2. Is stock greater than zero?
  3. Is stock equal to five?
  4. Is the product price below ₹2,000?

Print every result.

Extra Challenge

Create:

const enteredPin = "1234";
const savedPin = "1234";

Check whether they match using strict equality.

Then change:

const enteredPin = 1234;

Run the comparison again.

Explain why the result changes.

Frequently Asked Questions

What are comparison operators in JavaScript?

Comparison operators compare values and return a boolean result: true or false.

What are the main comparison operators in JavaScript?

The main comparison operators are ===, !==, >, <, >=, and <=.

JavaScript also has loose comparison operators == and !=.

What is === in JavaScript?

=== is the strict equality operator. It checks whether two values have the same value and the same type.

What is the difference between == and ===?

== may convert values before comparing them.

=== compares both value and type without loose type conversion.

For most modern beginner code, prefer ===.

What is the difference between != and !==?

!= performs loose inequality comparison and may convert types.

!== checks strict inequality without loose type conversion.

What does > mean in JavaScript?

> checks whether the left value is greater than the right value.

10 > 5

returns true.

What does < mean in JavaScript?

< checks whether the left value is less than the right value.

3 < 8

returns true.

What does >= mean in JavaScript?

>= checks whether the left value is greater than or equal to the right value.

What does <= mean in JavaScript?

<= checks whether the left value is less than or equal to the right value.

Do comparison operators return numbers?

No. Comparison operators return boolean values: true or false.

Why is 5 === "5" false?

5 is a number and "5" is a string.

Strict equality requires both value and type to match.

Can JavaScript compare strings?

Yes. Strict equality can check whether strings match exactly.

"hello" === "hello"

returns true.

String comparisons are case-sensitive.

Why are two identical-looking objects not equal with ===?

Two separately created objects have different references, even when their properties look the same.

What should I learn after comparison operators?

Learn JavaScript logical operators next. They let you combine comparisons using &&, ||, and !.

Summary

JavaScript comparison operators compare values and return:

true

or:

false

The main operators are:

===   !==   >   <   >=   <=

For most modern JavaScript code, prefer:

===
!==

over loose equality operators:

==
!=

You learned how to compare:

  • Numbers
  • Strings
  • Booleans
  • null and undefined
  • Objects
  • Arrays
  • Form-like values

You also learned why data types matter during comparisons.

For example:

5 === "5"

returns:

false

because one value is a number and the other is a string.

Comparison operators prepare you for JavaScript conditions because conditions depend on true-or-false results.

Continue Learning JavaScript

Previous Lesson: JavaScript Operators Explained

Course Home: JavaScript Tutorial for Beginners

Next Lesson: JavaScript Logical Operators Explained

In the next lesson, you will learn how &&, ||, and ! combine or reverse comparisons so your code can handle more than one condition at a time.

James Parker

James Parker

James Parker is a technology writer and WordPress enthusiast who shares practical guides on SEO, web development, AI tools, and digital growth. He focuses on creating easy-to-follow tutorials that help beginners and professionals build better websites and improve their online presence.