===) is only true if the operands are of 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 to the same type, before comparison.
Strings are compared based on standard lexicographical ordering, using Unicode values.
Features of comparisons:
trueor both are
x == y
1 == 1 // true "1" == 1 // true 1 == '1' // true 0 == false // true 0 == null // false 0 == undefined // false null == undefined // true
x != y
1 != 2 // true 1 != "1" // false 1 != '1' // false 1 != true // false 0 != false // false
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
x === y
3 === 3 // true 3 === '3' // false
The non-identity operator returns true if the operands are not equal and/or not of the same type.
x !== y
3 !== '3' // true 4 !== 3 // true
The greater than operator returns true if the left operand is greater than the right operand.
x > y
4 > 3 // true
The greater than or equal operator returns true if the left operand is greater than or equal to the right operand.
x >= y
4 >= 3 // true 3 >= 3 // true
The less than operator returns true if the left operand is less than the right operand.
x < y
3 < 4 // true
The less than or equal operator returns true if the left operand is less than or equal to the right operand.
x <= y
3 <= 4 // true
The standard equality operators (
!=) 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 (
!==) 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
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.
Numbertype value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest
Boolean, the Boolean operand is converted to 1 if it is
trueand +0 if it is
Numbervalue, using the
toStringmethods of the objects. If this attempt to convert the object fails, a runtime error is generated.
// true as both operands are Type String (i.e. string primitives): 'foo' === 'foo' var a = new String('foo'); var b = new String('foo'); // 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 === b // true as a and 'foo' are of different type and, the Object (a) // is converted to String 'foo' before comparison a == 'foo'