Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Comparison Operators

Comparison operators help you decide if two values are equal, greater, or less than each other.

They are the backbone of conditionals, filters, validation rules, and sorting logic.

Why We Need It

Small comparison mistakes can flip logic or let invalid data through. That is why strict equality and clear comparisons matter.

Understanding how JavaScript compares values keeps your conditions predictable and safe.

Syntax

a == b   // loose equality
a === b  // strict equality
a != b   // loose inequality
a !== b  // strict inequality
a > b, a < b, a >= b, a <= b

Basic Example

1. Strict vs Loose Equality

console.log(5 == "5");   // true
console.log(5 === "5");  // false
console.log(0 == false);  // true
console.log(0 === false); // false

Strict equality avoids type coercion and is safer for most comparisons.

Real World Example

2. Relational Operators

console.log(10 > 7);   // true
console.log(10 <= 7);  // false
console.log("b" > "a"); // true (lexicographic)

Relational operators compare numeric values and also work with strings.

Multiple Use Cases

Equality vs Strict Equality

`==` allows type coercion, while `===` compares both value and type. Most codebases prefer strict equality to avoid surprises.

Loose equality has edge cases like `0 == false` or `"" == false`, which can introduce subtle bugs.

Relational Comparisons

Relational operators like `>`, `<`, `>=`, and `<=` compare numeric values and also work with strings by lexicographic order.

Be careful when comparing strings that look like numbers. Convert explicitly if needed.

Special Values

`NaN` is never equal to itself; use `Number.isNaN()` or `Object.is()` to check it.

`null` and `undefined` are only loosely equal to each other; strict equality keeps them distinct.

Comparison in Collections

Comparisons appear in array filtering, sorting, and searching. Your comparisons determine what users see first.

Use comparators that return consistent results to avoid unstable sorting behaviors.

More Examples

3. NaN and Object.is

const value = NaN;
console.log(value === NaN);         // false
console.log(Number.isNaN(value)); // true
console.log(Object.is(value, NaN)); // true

Use Number.isNaN or Object.is to safely compare NaN.

4. Sorting with Comparisons

const scores = [12, 4, 18, 6];

scores.sort((a, b) => a - b);
console.log(scores); // [4, 6, 12, 18]

Comparators rely on consistent comparison results for correct sorting.

Comparison

Without

console.log(0 == false);  // true
console.log("" == false); // true

With

console.log(0 === false);  // false
console.log("" === false); // false

Common Mistakes and Fixes

Using == by default

Prefer === to avoid coercion surprises.

Comparing NaN with ===

Use Number.isNaN or Object.is for NaN checks.

Sorting strings like numbers

Convert strings to numbers before comparison.

Assuming null and undefined are the same

Use strict equality to keep them distinct.

Interview Questions

Why prefer strict equality?

It avoids implicit type coercion and makes comparisons predictable.

How do you compare NaN?

Use Number.isNaN or Object.is because NaN !== NaN.

How do you compare objects?

Object equality compares references, not deep values.

Practice Problem

Practice: Write a function that checks if a user is an adult and the score is in range using comparisons.

const age = 17;
const score = 92;
// TODO: print true only if age >= 18 and score between 80 and 100

One Possible Solution

const age = 17;
const score = 92;
const ok = age >= 18 && score >= 80 && score <= 100;
console.log(ok);

Frequently Asked Questions

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

`==` coerces types before comparing; `===` compares both value and type.

Why is NaN not equal to itself?

NaN represents an invalid number, and by specification it is not equal to anything, including itself.

Can I compare objects with ===?

Yes, but it compares references. Two different objects with the same contents are not equal.

How do string comparisons work?

JavaScript compares strings lexicographically based on Unicode code points.

Try It Yourself

Try the comparisons in the compiler and see how == and === behave.