Difference between === and == in Javascript.

JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===) 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.
This means that, the == operator will compare for equality after doing any necessary type conversions. The ===operator will not do the conversion, so if two values are not the same type === will simply return false. From performance point of view, It’s this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.
Example:

1 == 1    // true
1 == “1”  //true
1 == ‘1’  //true
1 === 1   //true
1 === “1” //false
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:
  • 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.

References: Stack Overflow
Comparison Operators

 

Leave a comment