JavaScript compares two values with the equality operators ==, ===, != and !==, and the relational operators <, >, <= and >=. Use === and !== for equality because they do not coerce types, and use the relational operators to order numbers, strings and other comparable values.
The original question lists the symbols <, >, == and ><. Note that >< is not a JavaScript operator, and <> (from SQL or Pascal) is not valid either. Use !== for "not equal".
| Operator | Meaning | Coerces types? | Example | Result |
|---|---|---|---|---|
== | Loose equal | Yes | 5 == "5" | true |
!= | Loose not equal | Yes | 5 != "5" | false |
=== | Strict equal | No | 5 === "5" | false |
!== | Strict not equal | No | 5 !== "5" | true |
Strict equality is the recommended default. NaN is never equal to anything, including itself, so test it with Number.isNaN() or Object.is().
The relational operators compare order:
There are no strict versions of these. If both operands are strings, they are compared by UTF-16 code unit order. Otherwise they are converted to numbers:
Comparing numeric strings as strings is a classic bug. Convert first with Number(value), or sort with a comparator.
Objects are compared by reference, not by content:
The relational operators work on dates because dates convert to timestamps, but equality needs getTime(). For deep comparison of data, compare fields or use a utility such as node:util isDeepStrictEqual in Node.js.
Operators compare code units, so accented or case-sensitive text does not sort the way people expect. Use localeCompare or Intl.Collator:
Conditions such as if (value) do not use the equality operators. They convert the value to a boolean, and these values are falsy: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy. Mixing the two ideas causes bugs:
Both lines for [] are correct, which shows why loose equality is hard to predict. Compare explicitly, for example list.length === 0.
=== and !== by default.value == null only as a shortcut for null or undefined.===.eqeqeq rule to enforce this.<> in JavaScript?No. Use !== for strict inequality or != for loose inequality.
== converts operand types before comparing, and === requires the same type and value. === is more predictable.
By the IEEE 754 standard, NaN is not equal to any value, including itself. Use Number.isNaN(x).
1 < x < 10?It parses as (1 < x) < 10, which compares a boolean to 10 and is almost always wrong. Write 1 < x && x < 10.
==, !=), strict (===, !==) and relational (<, >, <=, >=) operators.<> or >< operators.