JavaScript Comparison Operators

JavaScript comparison operators compare two values and return a Boolean result: true or false. The most important distinction is between strict equality (===, !==) and loose equality (==, !=), which performs type coercion.

Simple example:

const value = 5;
console.log(value === 5);   // true
console.log(value === "5"); // false
console.log(value == "5");  // true because == coerces types

For new application code, strict equality is usually the safer default because it avoids many implicit-conversion surprises.

JavaScript Comparison Operators Top ↑

OperatorMeaningExample
===Strict equal5 === 5 → true
!==Strict not equal5 !== "5" → true
==Loose equal with coercion5 == "5" → true
!=Loose not equal with coercion5 != "5" → false
<Less than4 < 5 → true
>Greater than6 > 5 → true
<=Less than or equal5 <= 5 → true
>=Greater than or equal5 >= 5 → true

Strict Equality with === Top ↑

=== compares values without converting one operand to another type first.

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

This corrects a contradictory comment in the old example where 5 == "5" was described as false even though loose equality makes it true.

Loose Equality with == and Type Coercion Top ↑

== performs coercion according to JavaScript's abstract equality rules. That can be intentional in specialized code, but it is easier to misread.

console.log(5 == "5");        // true
console.log(false == 0);      // true
console.log("" == 0);         // true
console.log(null == undefined); // true
Recommended default: prefer === and !==. If you deliberately use loose equality, do so because you understand the exact coercion behavior required.

Not Equal: !== and != Top ↑

const value = 5;
console.log(value !== "5"); // true
console.log(value != "5");  // false
console.log(value !== 4);   // true

Less Than, Greater Than and Inclusive Comparisons Top ↑

The relational operators compare ordering. They are common in loops, ranges, validation, and sorting logic.

const score = 75;

console.log(score > 50);   // true
console.log(score >= 75);  // true
console.log(score < 100);  // true
console.log(score <= 74);  // false

Mixed-type relational comparisons can also trigger conversion, so normalize input types before comparing user-entered numeric data.

Comparing Strings Top ↑

Strings are compared lexicographically using Unicode code-unit ordering, which is case-sensitive and is not the same as locale-aware human sorting.

console.log("apple" < "banana"); // true
console.log("Z" < "a");          // true in code-unit ordering
console.log("10" < "2");         // true as strings

For user-facing language sorting, consider localeCompare() or Intl.Collator instead of assuming < gives natural-language order.

Comparing NaN Top ↑

NaN is unusual because it is not equal to itself under either === or ==. Use Number.isNaN() when you need to detect it.

const result = Number("not a number");
console.log(result === NaN);       // false
console.log(Number.isNaN(result)); // true

Comparing Objects and Arrays Top ↑

Objects and arrays are compared by reference identity, not by recursively comparing their contents.

const a = { score: 10 };
const b = { score: 10 };
const c = a;

console.log(a === b); // false
console.log(a === c); // true

If you need structural equality, compare the relevant fields or use a deliberately chosen deep-comparison strategy.

Object.is() and Special Values Top ↑

Object.is() is another identity comparison operation. It differs from === mainly for NaN and signed zero.

console.log(Object.is(NaN, NaN)); // true
console.log(NaN === NaN);          // false
console.log(Object.is(0, -0));      // false
console.log(0 === -0);              // true

Do Not Chain Comparisons Like Python Top ↑

JavaScript evaluates a < b < c from left to right. The first comparison becomes a Boolean and that Boolean is then compared with c, which is rarely what was intended.

const x = 15;
const inRange = 10 < x && x < 20;
console.log(inRange); // true

Using Comparisons in if Conditions Top ↑

const age = 20;

if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

Combine comparisons with logical operators when more than one condition must be tested, and see if...else for conditional execution.

Common Comparison Mistakes Top ↑

  • Using assignment (=) when equality (===) was intended.
  • Assuming == and === behave the same.
  • Comparing numeric form input as strings without converting or validating it.
  • Expecting two separate objects with identical properties to be strictly equal.
  • Checking NaN with === NaN.
  • Writing chained mathematical comparisons such as 0 < x < 10.

Continue with logical operators, if...else, typeof, and variables.




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer