What is the difference between == and === operators

JavaScript provides both strict(===, !==) and type-converting(==, !=) equality comparison. The strict operators take type of variable in consideration, while non-strict operators make type correction/conversion based upon values of variables. The strict operators follow the below conditions for different types,

  1. Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  2. Two numbers are strictly equal when they are numerically equal. i.e, Having the same number value. There are two special cases in this,
  • NaN is not equal to anything, including NaN.
  • Positive and negative zeros are equal to one another.
  1. Two Boolean operands are strictly equal if both are true or both are false.
  2. Two objects are strictly equal if they refer to the same Object.
  3. Null and Undefined types are not equal with ===, but equal with ==. i.e, null===undefined --> false but null==undefined --> true

Below are the important difference points:

  1. == in JavaScript is used for comparing two variables, but it ignores the datatype of variable whereas === is used for comparing two variables, but this operator also checks datatype and compares two values.
  2. == checks the equality of two operands without considering their type whereas === compares equality of two operands with their types.
  3. == return true if the two operands are equal. It will return false if the two operands are not equal whereas === returns true only if both values and data types are the same for the two variables.
  4. == make type correction based upon values of variables whereas === takes type of variable in consideration.
1 Like