Comparison Operators

A comparison hands back a boolean

A comparison operator takes two values, asks a yes-or-no question about them, and hands back a boolean: either true or false. JavaScript spells both of those in lowercase, and a capital letter would make them something else entirely.
The operators fall into two families. One family puts values in order. The other tests whether two values match, and it is larger than you would expect, because matching comes in a loose flavour and a strict flavour. The table below is the whole list.
OperatorThe question it asks
a < bIs a less than b?
a > bIs a greater than b?
a <= bIs a less than b, or equal to it?
a >= bIs a greater than b, or equal to it?
a == bDo a and b match, converting types first if they differ?
a != bDo a and b fail to match, converting types first if they differ?
a === bDo a and b have the same type AND the same value?
a !== bDo a and b differ in type, or in value, or in both?

The loose pair converts, the strict pair refuses

This is where beginners lose the most time. When the two sides have different types, == quietly converts one of them and then compares. === will not convert: if the types differ it answers false immediately, without ever looking at the values.

The result is a value like any other

A comparison is worked out before anything else happens to it, and the boolean it produces can be stored in a variable, printed, or passed along:
let roomy = 62 > 45;
console.log(roomy);
Whatever the two sides were, and whichever of the operators in the table you used, what comes back is one of the two booleans. Never a number, never a piece of text.
If you want to know what KIND of value you are holding rather than which value it is, hand it to typeof. It answers with a single plain word, and console.log prints that word on its own with no quote marks around it:
console.log(typeof 62);     // number
console.log(typeof "45"); // string
The program below checks whether a box fits a gap on a shelf. It also compares the box's measured width with the width printed on its label, which is a string. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.
• Set width to 36, the same as gap.
• Delete the quote marks around "34", so that label holds a number.
• Change width === label to width === Number(label).
let gap = 36;
let width = 34;
let label = "34";
let fits = width <= gap;
console.log("Fits:", fits);
console.log("Smaller than the gap:", width < gap);
console.log("Loose match:", width == label);
console.log("Strict match:", width === label);
console.log(typeof width, typeof label);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.