Comparison Operators

Summary

Operators
Implemented in JavaScript 1.0

JavaScript 1.3: Added the === and !== operators.

ECMAScript Edition ECMAScript 1st Edition includes all comparison operators except === and !==.

ECMAScript 3rd Edition adds === and !==.

JavaScript has both strict and Type–converting (abstract) comparisons. A strict comparison (e.g., ===) is only true if the operands are the same Type. The more commonly used abstract comparison (e.g., ==) converts the operands to the same Type before making the comparison. For relational abstract comparisons (e.g., <=), the operands are first converted to primitives, then the same Type, before comparison.

Strings are compared based on standard lexicographical ordering, using Unicode values.

Features of comparisons:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two distinct objects are never equal for either strictly or abstract comparisons.
  • An expression comparing Objects is only true if the operands reference the same Object.
  • Null and Undefined Types are == (but not ===).

The following table describes the comparison operators:

Operator Description Examples returning true1
Equal (==) If the two operands are not of the same type, JavaScript converts the operands, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison. If both operands are objects, then JavaScript compares internal references which are not equal when operands refer to different objects in memory.

var1 != 4
var1 != "5"

Strict equal (===) Returns true if the operands are strictly equal (see above) with no type conversion.

3 === var1

Strict not equal (!==) Returns true if the operands are not equal and/or not of the same type.

var2 !== 3
3 !== '3'

Greater than (>) Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1
var1 >= 3

Less than (<) Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.

Using the Equality Operators

The standard equality operators (== and !=) use the Abstract Equality Comparison Algorithm to compare two operands. If the operands are of different Types, it will attempt to convert them to the same Type before making the comparison, e.g., in the expression 5 == '5', the string on the right is converted to Number before the comparison is made.

The strict equality operators (=== and !==) use the Strict Equality Comparison Algorithm and are intended for performing equality comparisons on operands of the same Type. If the operands are of different Types, the result is always false so 5 !== '5'.

Use strict equality operators if the operands must be of a specific Type as well as value or if the exact Type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same Type.

When Type conversion is involved in the comparison (i.e., non–strict comparison), JavaScript converts Type String, Number, Boolean, or Object operands as follows:

  • When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
  • If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
  • If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a runtime error is generated.
  • Note that an object is converted into a primitive if, and only if, its comparand is a primitive. If both operands are objects, they're compared as objects, and the equality test is true only if both refer the same object.
Note: String Objects are Type Object, not String! String objects are rarely used, so the following results might be surprising:
'foo' === 'foo' // true as both operands are Type String (i.e. string primitives)
var a = new String('foo');
var b = new String('foo');
a == b // false as a and b are Type Object and reference different objects
a === b // false as a and b are Type Object and reference different objects
a == 'foo' // true as a and 'foo' are of different type and, the Object (a) is converted to String 'foo' before comparison

Document Tags and Contributors

Last updated by: Havvy,