JavaScript Comparison Operators

Comparison operators compare two values and return a boolean (true or false). They are essential for conditional statements and decision-making in your code.

Comparison Operators Overview

JavaScript provides 8 comparison operators. The most important distinction is between == (loose equality) and === (strict equality).

OperatorDescriptionExampleResult
==Equal to (loose)5 == "5"true
===Equal to (strict)5 === "5"false
!=Not equal (loose)5 != "5"false
!==Not equal (strict)5 !== "5"true
>Greater than10 > 5true
<Less than3 < 8true
>=Greater or equal5 >= 5true
<=Less or equal3 <= 2false

Loose vs Strict Equality

The == operator converts types before comparing (type coercion). The === operator checks both value AND type without conversion. Always prefer === to avoid unexpected results.

== vs ===
// Loose equality (==) performs type coercion
console.log(5 == "5");    // true (string converted to number)
console.log(0 == false);   // true (false converted to 0)
console.log("" == false);  // true (both coerce to 0)
console.log(null == undefined); // true (special rule)

// Strict equality (===) checks type AND value
console.log(5 === "5");    // false (different types)
console.log(0 === false);   // false (different types)
console.log("" === false);  // false (different types)
console.log(null === undefined); // false (different types)

Comparing Strings

Strings are compared character by character using their Unicode values. Uppercase letters have lower Unicode values than lowercase letters.

String Comparisons
console.log("apple" < "banana");  // true (alphabetical)
console.log("cat" > "car");      // true ('t' > 'r')
console.log("abc" === "abc");    // true

// Case matters!
console.log("A" < "a");    // true (uppercase < lowercase)
console.log("Z" < "a");    // true

// Length matters when prefix matches
console.log("app" < "apple"); // true (shorter string is less)

// For case-insensitive comparison:
let str1 = "Hello";
let str2 = "hello";
console.log(str1.toLowerCase() === str2.toLowerCase()); // true

Comparing Different Types

When comparing different types with ==, JavaScript converts them to numbers. With ===, different types are always unequal. Be aware of quirks with null and undefined.

Type Comparison Quirks
// String vs Number
console.log("10" > 9);   // true ("10" becomes 10)
console.log("2" > "12"); // true (string comparison: '2' > '1')

// null comparisons
console.log(null == 0);         // false
console.log(null == undefined); // true
console.log(null === undefined); // false

// NaN is never equal to anything
console.log(NaN == NaN);  // false
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true (correct way to check)

The Ternary Operator

The ternary operator is a shorthand for if/else. It uses the syntax: condition ? valueIfTrue : valueIfFalse. It is the only JavaScript operator that takes three operands.

Ternary Operator
let age = 20;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // "Adult"

let score = 75;
let grade = score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F";
console.log(grade); // "C"

// Using ternary in template literals
let items = 3;
console.log(`You have ${items} item${items !== 1 ? "s" : ""}`);
// "You have 3 items"
📝 Note: Always use === and !== instead of == and !=. Strict equality avoids confusing type coercion bugs. The only common exception is checking for null/undefined with == null, which catches both null and undefined.
Exercise:
What does 0 === false return?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.