JavaScript comparison operators: ==, ===, and ordering

JavaScript comparisons return true or false. Use === and !== to compare without type coercion. For ordering, check whether the values are strings or numbers. !== is true whenever strict equality is false.

Common operators and strict equality

Operator Meaning Example result
=== Strict equality 5 === "5" → false
!== Strict inequality 5 !== 6 → true
== Equality with coercion when required 5 == "5" → true
!= Negation of loose equality 5 != "5" → false
<·> Less than or greater than 2 < 5 → true
<=·>= Less than or equal to, or greater than or equal to 5 >= 5 → true

=== returns false between different types and compares objects by reference. !== negates that result, so two numbers of different values also pass. == can coerce values according to specific rules. Convert explicitly when the desired type is known.

5 === 5;   // true
5 !== 6;   // true: both numbers, different values
5 !== "5"; // true: different types
5 != "5";  // false: loosely equal

Order numeric-looking strings correctly

"12" < "2"; // true
12 < 2;     // false
"12" < 2;   // false

Two strings are compared by UTF-16 code units. "12" < "2" is true because "1" comes before "2". This is different from numeric order and can differ from linguistic sorting. With mixed strings and numbers, < may use numeric conversion. Validate input before relying on it.

Compare text and numbers in a complete example

Save the full code as compare-values.html and open it. A form input’s value is a string. The example compares those strings and their converted numbers, using == to demonstrate coercion.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare strings and numbers</title>
</head>
<body>
<h1>Compare input values</h1>
<label for="left">Left value</label><input id="left" value="12">
<label for="right">Right value</label><input id="right" value="2">
<button id="compare" type="button">Compare</button>
<button id="restore" type="button">Restore defaults</button>
<pre id="output"></pre>
<script>
const left = document.getElementById("left");
const right = document.getElementById("right");
const output = document.getElementById("output");
function compare() {
  const a = left.value.trim();
  const b = right.value.trim();
  if (a === "" || b === "") {
    output.textContent = "Enter both values.";
    return;
  }
  const x = Number(a);
  const y = Number(b);
  if (!Number.isFinite(x) || !Number.isFinite(y)) {
    output.textContent = "Enter values that convert to finite numbers.";
    return;
  }
  output.textContent = [
    `Strings a < b: ${a < b}`,
    `Numbers x < y: ${x < y}`,
    `String and number a == x: ${a == x}`,
    `String and number a === x: ${a === x}`,
    `Numbers x === y: ${x === y}`
  ].join("\n");
}
document.getElementById("compare").addEventListener("click", compare);
document.getElementById("restore").addEventListener("click", () => {
  left.value = "12";
  right.value = "2";
  compare();
});
compare();
</script>
</body>
</html>

With 12 and 2, a < b is true and x < y is false. The text a loosely equals the number x, but does not strictly equal it. Change only the right value to 12 to get x === y. Blank values trigger an input prompt, and abc or Infinity fail the finite number check. Use Restore defaults to repeat. This example accepts JavaScript’s number syntax and does not limit it to decimal input.

Handle NaN, objects, and ranges

NaN === NaN;      // false
NaN < 10;         // false
NaN >= 10;        // false
Number.isNaN(NaN); // true

const first = { count: 1 };
const second = { count: 1 };
first === second; // false
first === first;  // true

Detect NaN with Number.isNaN(). To compare the contents of objects, decide which properties matter. === does not traverse them.

const value = 15;

// Check both range boundaries
const inRange = 10 <= value && value <= 20;
console.log(inRange); // true

Use two comparisons joined by && for a range. 10 <= value <= 20 compares a Boolean with 20 in its second step. Choose < or <= depending on whether the endpoint belongs in the range.

Distinguish NaN and signed zero

Run the code below in the Console to compare === with Object.is(). Both avoid coercion. Object.is() treats NaN as equal to itself and distinguishes positive and negative zero.

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

  const item = { quantity: 1 };
  console.log(Object.is(item, item));            // true
  console.log(Object.is(item, { quantity: 1 })); // false
}

The lines show false true, true false, true, and false. Use === for ordinary comparisons. Use Object.is() if you need NaN values to compare equal or need to preserve the sign of zero. Objects are still compared by reference.