strict vs type–converting comparisons
|Strict comparision||Type converting comparison|
|1. A strict comparison (e.g., ===) is only true if the operands are of the same type.||1. abstract comparison (e.g. ==) converts the operands to the same Type before making the comparison.|
More 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 strict or abstract comparisons.
- An expression comparing Objects is only true if the operands reference the same Object.
- Null and Undefined Types are strictly equal to themselves and abstractly equal to each other.
//syntax x == y
//examples 1 == 1 // true "1" == 1 // true 1 == '1' // true 0 == false // true 0 == null // false 0 == undefined // false null == undefined // true
//syntax x != y
//examples 1 != 2 // true 1 != "1" // false 1 != '1' // false 1 != true // false 0 != false // false
Identity / strict equality (===)
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
//syntax x === y
//examples 3 === 3 // true 3 === '3' // false
Non-identity / strict inequality (!==)
The non-identity operator returns true if the operands are not equal and/or not of the same type.
//syntax x !== y
//examples 3 !== '3' // true 4 !== 3 // true
Greater than operator (>)
The greater than operator returns true if the left operand is greater than the right operand.
//syntax x > y
//examples 4 > 3 // true
Greater than or equal operator (>=)
The greater than or equal operator returns true if the left operand is greater than or equal to the right operand.
//syntax x >= y
//examples 4 >= 3 // true 3 >= 3 // true
Less than operator (<)
The less than operator returns true if the left operand is less than the right operand.
//syntax x < y
//examples 3 < 4 // true
Less than or equal operator (<=)
The less than or equal operator returns true if the left operand is less than or equal to the right operand.
//syntax x <= y
//examples 3 <= 4 // true
- If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
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.