JavaScript Comparison: Operators, Examples & Guide

Learn JavaScript Comparison operators including ==, ===, !=, !==, >, <, >=, and <= with clear examples, type coercion, best practices, and common mistakes.

JavaScript Comparison

JavaScript comparison is the process of checking two values and determining how they relate to each other. A comparison can tell us whether one value is greater than another, whether two values are equal, or whether they are different.

Comparison operators are used heavily in JavaScript programs. They are especially important in decision-making, such as if statements, loops, validation, filtering, searching, and conditional expressions.

For example:

let age = 20;

console.log(age >= 18);

Output:

true

Here, JavaScript checks whether age is greater than or equal to 18. Because 20 satisfies the condition, the result is true.

What Is a Comparison Operator?

A comparison operator compares two values and returns a Boolean result.

A Boolean value can only be:

true

or:

false

For example:

let a = 10;
let b = 20;

console.log(a < b);

Output:

true

The expression asks whether 10 is less than 20.

JavaScript provides several comparison operators.

OperatorMeaning
==Equal to
===Strictly equal to
!=Not equal to
!==Strictly not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

These operators do not normally change the values being compared. Instead, they produce a Boolean result.

Equal to (==)

The double-equals operator checks whether two values are equal after JavaScript performs type conversion when necessary.

console.log(10 == 10);

Output:

true

It can also consider different data types equal:

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

Output:

true

The number 10 and the string "10" have different types, but loose equality converts values during comparison.

Another example:

console.log(true == 1);

Output:

true

Because of this automatic type conversion, == can sometimes produce results that are surprising.

Strictly Equal to (===)

The triple-equals operator checks both the value and the data type.

console.log(10 === 10);

Output:

true

But:

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

Output:

false

The first value is a number, while the second value is a string.

This distinction makes === more predictable.

For example:

let age = 18;

console.log(age === 18);

The result is:

true

But:

let age = "18";

console.log(age === 18);

The result is:

false

In modern JavaScript, === is generally preferred when you want an exact comparison without implicit type conversion.

Not Equal to (!=)

The != operator checks whether two values are different after possible type conversion.

console.log(10 != 20);

Output:

true

But:

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

Output:

false

This happens because loose inequality follows the same general type-conversion rules as ==.

Strictly Not Equal to (!==)

The !== operator checks whether two values are different in value or type.

console.log(10 !== 20);

Output:

true

And:

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

Output:

true

The values look similar, but their types are different.

typeof 10

returns:

"number"

while:

typeof "10"

returns:

"string"

Therefore:

10 !== "10"

is true.

Greater Than (>)

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

console.log(20 > 10);

Output:

true

But:

console.log(10 > 20);

Output:

false

It is often useful for checking limits.

let marks = 75;

if (marks > 50) {
    console.log("Pass");
}

Less Than (<)

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

console.log(10 < 20);

Output:

true

For example:

let temperature = 15;

if (temperature < 20) {
    console.log("It is cool.");
}

Greater Than or Equal to (>=)

The >= operator checks whether a value is greater than or equal to another value.

console.log(20 >= 10);

Output:

true

It also returns true when both values are equal:

console.log(20 >= 20);

Output:

true

A common example is checking a minimum age:

let age = 18;

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

Less Than or Equal to (<=)

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

console.log(10 <= 20);

Output:

true

It also returns true when the values are equal:

console.log(20 <= 20);

Output:

true

For example:

let score = 40;

if (score <= 50) {
    console.log("The score is within the limit.");
}

Comparison Operators With Variables

Comparison operators are commonly used with variables.

let price = 500;
let budget = 1000;

console.log(price < budget);

The result is:

true

Another example:

let username = "Dibya";

console.log(username === "Dibya");

Output:

true

Variables make comparisons useful in real applications because their values can change while the program runs.

Comparison in if Statements

One of the most common uses of comparison operators is with if statements.

let age = 21;

if (age >= 18) {
    console.log("You are an adult.");
}

The condition:

age >= 18

produces true, so the code inside the if block runs.

If the condition is false, the block is skipped.

let age = 15;

if (age >= 18) {
    console.log("You are an adult.");
}

Nothing is printed because the condition is false.

Comparison With if...else

Comparison becomes even more useful when combined with else.

let marks = 35;

if (marks >= 40) {
    console.log("Pass");
} else {
    console.log("Fail");
}

Since 35 >= 40 is false, JavaScript executes the else block.

Multiple Comparisons With else if

You can test multiple conditions using else if.

let marks = 85;

if (marks >= 90) {
    console.log("Excellent");
} else if (marks >= 75) {
    console.log("Very Good");
} else if (marks >= 50) {
    console.log("Good");
} else {
    console.log("Needs Improvement");
}

JavaScript checks the conditions from top to bottom and executes the first matching branch.

Comparison and Logical Operators

Comparison operators are often combined with logical operators.

The main logical operators are:

  • && — AND
  • || — OR
  • ! — NOT

For example:

let age = 25;
let hasID = true;

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

Both conditions must be true.

Another example:

let day = "Sunday";

if (day === "Saturday" || day === "Sunday") {
    console.log("Weekend");
}

At least one condition must be true.

Comparing Strings

JavaScript can compare strings.

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

Output:

true

String comparisons are case-sensitive.

console.log("JavaScript" === "javascript");

Output:

false

The uppercase J and lowercase j are different characters.

JavaScript can also use relational operators with strings:

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

String relational comparison is based on the characters’ ordering according to JavaScript’s string comparison rules.

Comparing Numbers

Number comparisons are straightforward.

let x = 100;
let y = 50;

console.log(x > y);

Output:

true

You can also compare decimal numbers:

console.log(10.5 > 10.2);

Output:

true

However, floating-point arithmetic can sometimes create unexpected results because JavaScript uses binary floating-point representation for ordinary Number values.

For example:

console.log(0.1 + 0.2 === 0.3);

The result is:

false

This is not a comparison-operator bug. It is related to how binary floating-point numbers represent certain decimal fractions.

Comparing Booleans

Boolean values can also be compared.

console.log(true === true);

Output:

true

And:

console.log(false === true);

Output:

false

When checking whether a variable is a Boolean, strict comparison is usually clearer:

let loggedIn = true;

if (loggedIn === true) {
    console.log("User is logged in.");
}

In many cases, however, you can simply write:

if (loggedIn) {
    console.log("User is logged in.");
}

== vs ===

Understanding the difference between == and === is one of the most important parts of JavaScript comparison.

Consider:

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

This produces:

true

But:

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

produces:

false

The first comparison allows type conversion. The second does not.

A simple way to remember it is:

==   → loose equality
===  → strict equality

For most application code, === and !== are preferable because they make the intended comparison more explicit.

Type Conversion During Loose Comparison

The loose equality operator follows JavaScript’s abstract equality comparison rules. This means JavaScript may convert one value before comparing it.

For example:

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

returns:

true

But:

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

returns:

false

Loose comparisons can become complicated when different types are involved. For that reason, relying on == without understanding coercion can lead to unexpected behavior.

null and undefined

Special values such as null and undefined deserve attention.

With loose equality:

console.log(null == undefined);

the result is:

true

But:

console.log(null === undefined);

returns:

false

They are different JavaScript values and different types.

typeof null

returns "object" because of a long-standing JavaScript behavior, while:

typeof undefined

returns "undefined".

NaN and Comparison

NaN means “Not-a-Number.” It represents an invalid or unrepresentable numeric result.

A surprising property of NaN is that it is not equal to itself.

console.log(NaN === NaN);

Output:

false

Even:

console.log(NaN == NaN);

returns:

false

To reliably test whether a value is NaN, use:

Number.isNaN(value);

For example:

let result = Number("hello");

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

Output:

true

Comparing Objects

Objects require special attention.

Consider:

let a = { name: "John" };
let b = { name: "John" };

console.log(a === b);

The result is:

false

Although the objects contain the same property and value, they are two separate object references.

However:

let a = { name: "John" };
let b = a;

console.log(a === b);

returns:

true

Both variables refer to the same object.

This means === does not perform a deep comparison of object contents.

Comparing Arrays

Arrays behave similarly because arrays are objects.

let first = [1, 2, 3];
let second = [1, 2, 3];

console.log(first === second);

Output:

false

The arrays have the same contents but are different array objects.

If you need to compare array contents, you need to use an appropriate method or write a comparison routine.

For simple arrays, one possible approach is:

console.log(
    JSON.stringify(first) === JSON.stringify(second)
);

This approach is not suitable for every situation, especially when arrays contain complex values or property ordering matters.

Comparing Dates

Dates are also objects.

let date1 = new Date("2026-01-01");
let date2 = new Date("2026-01-01");

console.log(date1 === date2);

This returns:

false

To compare their represented time values, use:

console.log(date1.getTime() === date2.getTime());

This compares the numeric timestamps instead of the object references.

Truthy and Falsy Values

JavaScript conditions do not always require an expression that explicitly produces true or false.

JavaScript converts values to Boolean when they are used in a Boolean context.

Some commonly falsy values include:

false
0
-0
0n
""
null
undefined
NaN

Most other values are truthy, including empty arrays and empty objects.

For example:

let username = "";

if (username) {
    console.log("Username exists.");
} else {
    console.log("Username is empty.");
}

The empty string is falsy, so the else block executes.

Comparison and Type Coercion

Type coercion means converting a value from one type to another.

JavaScript can perform coercion automatically in some comparisons.

For example:

console.log("20" > 10);

This comparison involves a string and a number. JavaScript applies the rules for relational comparison and converts the string to a numeric value in this case.

The result is:

true

Because coercion rules differ depending on the operator and operand types, it is important to understand the specific comparison being performed.

Comparing Numeric Strings

Numeric strings can create confusing situations.

console.log("20" > "100");

Because both operands are strings, the comparison is performed lexicographically rather than as ordinary numeric comparison.

This results in:

true

because "20" comes after "10" in the relevant string ordering, even though the numeric value 20 is less than 100.

If the intention is to compare numbers, convert the values explicitly:

console.log(Number("20") > Number("100"));

This returns:

false

Explicit conversion often makes code easier to understand.

Comparison With switch

The switch statement is another way to make decisions based on values.

let day = "Sunday";

switch (day) {
    case "Saturday":
        console.log("Weekend");
        break;

    case "Sunday":
        console.log("Weekend");
        break;

    default:
        console.log("Weekday");
}

switch uses strict comparison semantics when matching cases.

Comparison in the Conditional Operator

The ternary operator allows a simple comparison and decision to be written in one expression.

let age = 20;

let result = age >= 18 ? "Adult" : "Minor";

console.log(result);

Output:

Adult

The structure is:

condition ? valueIfTrue : valueIfFalse

It is useful for short, simple decisions. For complicated logic, an if...else statement is usually easier to read.

Comparison in Loops

Comparison operators are essential for loops.

For example:

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

The condition:

i <= 5

determines whether the loop continues.

A while loop works in a similar way:

let count = 1;

while (count <= 5) {
    console.log(count);
    count++;
}

The comparison controls when the loop stops.

Comparison With User Input

Values received from forms or other input sources are often strings.

For example:

let age = prompt("Enter your age:");

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

JavaScript may coerce the string during the relational comparison.

A clearer approach is to convert the input explicitly:

let age = Number(prompt("Enter your age:"));

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

You should also consider invalid input:

let age = Number(prompt("Enter your age:"));

if (Number.isNaN(age)) {
    console.log("Please enter a valid number.");
} else if (age >= 18) {
    console.log("Eligible");
} else {
    console.log("Not eligible.");
}

Comparison With Form Values

HTML form controls commonly provide values as strings.

For example, if an input contains:

100

JavaScript may receive:

"100"

rather than:

100

Therefore, when numeric comparison is required, explicit conversion is often a good practice:

const score = Number(input.value);

if (score >= 50) {
    console.log("Pass");
}

Comparing Negative Numbers

Comparison operators work normally with negative numbers.

console.log(-5 < -2);

Output:

true

And:

console.log(-10 > -20);

Output:

true

Remember that a number closer to zero is greater than a more negative number.

Comparing BigInt Values

JavaScript also supports BigInt for integers larger than the safe integer range of ordinary Number.

For example:

const a = 9007199254740993n;
const b = 9007199254740992n;

console.log(a > b);

Output:

true

BigInt values can be compared using relational operators.

However, mixing BigInt and Number requires care, particularly with arithmetic operations. Equality comparisons also have specific behavior:

console.log(1n === 1);

returns:

false

because the types differ.

But:

console.log(1n == 1);

returns:

true

because loose equality permits the comparison according to its coercion rules.

Object.is() and Comparison

JavaScript also provides:

Object.is()

It is similar to strict equality but handles a few special cases differently.

For example:

console.log(Object.is(NaN, NaN));

returns:

true

Unlike:

console.log(NaN === NaN);

which returns:

false

Another difference involves signed zero:

console.log(Object.is(0, -0));

returns:

false

while:

console.log(0 === -0);

returns:

true

For ordinary comparisons, === is usually enough. Object.is() is useful when these special distinctions matter.

Common JavaScript Comparison Mistakes

Using = Instead of ===

A common mistake is confusing assignment with comparison.

if (age = 18) {
    // ...
}

The = operator assigns a value. It does not perform equality comparison.

Use:

if (age === 18) {
    // ...
}

when you want to compare.

Using == Without Understanding Coercion

This can lead to unexpected results:

console.log(0 == false);

Output:

true

If strict type matching is intended, use:

console.log(0 === false);

which returns:

false

Comparing Objects by Content With ===

This does not compare object contents:

{ a: 1 } === { a: 1 }

It compares object references, so the result is false.

Forgetting That Input Values Are Strings

A form input may provide "50" rather than 50.

Convert it when numerical operations are intended:

const value = Number(input.value);

Comparing Floating-Point Values Directly

Direct equality can be problematic with floating-point calculations.

Instead of assuming:

0.1 + 0.2 === 0.3

will be true, use an appropriate tolerance when approximate numeric equality is required:

const a = 0.1 + 0.2;
const b = 0.3;

console.log(Math.abs(a - b) < Number.EPSILON);

For larger calculations, choose a tolerance appropriate to the scale and precision required by the application.

Best Practices for JavaScript Comparison

Use strict equality when you need both value and type to match:

value === expected

Use strict inequality when you need to ensure that either the value or type differs:

value !== expected

Convert external data explicitly when its intended type is known:

const age = Number(input);

Keep conditions simple and readable:

if (age >= 18) {
    // ...
}

Avoid complicated chains of comparisons when a clearer data structure or separate logic would be easier to maintain.

Be especially careful when comparing:

  • null
  • undefined
  • NaN
  • objects
  • arrays
  • dates
  • numeric strings
  • floating-point numbers
  • BigInt values

Practical Examples

Checking Age

const age = 25;

if (age >= 18) {
    console.log("Eligible.");
} else {
    console.log("Not eligible.");
}

Checking a Password

const password = "secret123";

if (password === "secret123") {
    console.log("Password accepted.");
} else {
    console.log("Incorrect password.");
}

Checking a Score Range

const score = 78;

if (score >= 0 && score <= 100) {
    console.log("Valid score.");
} else {
    console.log("Invalid score.");
}

Checking a Number

const number = 12;

if (number > 0) {
    console.log("Positive number.");
} else if (number < 0) {
    console.log("Negative number.");
} else {
    console.log("Zero.");
}

Finding the Larger Number

const a = 45;
const b = 72;

if (a > b) {
    console.log("a is larger.");
} else if (b > a) {
    console.log("b is larger.");
} else {
    console.log("Both are equal.");
}

Using the Ternary Operator

const age = 22;

const status = age >= 18 ? "Adult" : "Minor";

console.log(status);

Comparing Multiple Conditions

const age = 30;
const country = "India";

if (age >= 18 && country === "India") {
    console.log("Condition satisfied.");
}

Quick Reference

OperatorExampleResult
==5 == "5"true
===5 === "5"false
!=5 != 10true
!==5 !== "5"true
>10 > 5true
<5 < 10true
>=10 >= 10true
<=5 <= 10true

Why JavaScript Comparison Is Important

Comparison is one of the basic building blocks of programming. Almost every interactive application needs to make decisions based on values.

A website may compare a user’s age before showing age-restricted content. A shopping website may compare a product price with a budget. A game may compare a player’s score with a winning score. A form may compare entered data against validation rules.

Without comparison operators, JavaScript would have very limited ability to make decisions.

Frequently Asked Questions

What does comparison mean in JavaScript?

Comparison means checking two values to determine their relationship. The result is normally a Boolean value, true or false.

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

== performs loose equality and may convert types before comparing. === performs strict equality and requires both the value and type to match.

Which is better, == or ===?

In most situations, === is the safer and clearer choice because it avoids unexpected type coercion.

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

!= performs loose inequality and may perform type conversion. !== performs strict inequality and considers both value and type.

Do comparison operators return Boolean values?

Yes. Comparison expressions normally produce either true or false.

console.log(10 > 5);

produces:

true

Can JavaScript compare strings?

Yes. JavaScript can compare strings for equality and ordering. String comparisons are case-sensitive.

Can JavaScript compare objects?

Yes, but === and related equality operators compare object references rather than the contents of separate objects.

How do I compare two dates?

Convert them to a common numeric representation, such as their timestamps:

date1.getTime() === date2.getTime()

Why is NaN === NaN false?

NaN has special numeric semantics in JavaScript and is not equal to itself. Use Number.isNaN() when you need to test specifically for NaN.

What should I remember about JavaScript comparison?

The most important points are:

  1. Comparison operators produce Boolean results.
  2. === checks both value and type.
  3. == allows type coercion.
  4. !== checks for strict inequality.
  5. >, <, >=, and <= compare ordering.
  6. Objects and arrays are compared by reference with equality operators.
  7. NaN requires special handling.
  8. Input values are often strings and may need conversion.
  9. Floating-point numbers require care when exact equality is expected.
  10. Clear, explicit comparisons make JavaScript code easier to understand and maintain.

JavaScript comparison operators may look simple, but understanding their behavior is essential for writing reliable programs. Once you are comfortable with strict equality, relational operators, type coercion, Boolean values, and special cases such as NaN and objects, you can build much more predictable conditions and decision-making logic in JavaScript.

Scroll to Top