Difference between == and ===

Difference between == and ===

What is Difference between == and ===

  • Both are used to compare two values but the only difference is that == compare only deals and give a boolean value actual whereas === will compare both deals and datatype and return true.

  • The === is also known as the Strictly equality operator.

  • The == is used when the data type of the operand isn't important and also know as Loose equality.

  • The === operand strictly compares two values, thus it is used in the places where the data type of the operand is important.

  • If we compare 5 with “5” using ===, then it will return a false value but if we compare the same value with == it will return true.

Example:

0 == false   // true
0 === false  // false, because they are of a different type
1 == "1"     // true, automatic type conversion for value only
1 === "1"    // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false

Understanding

  • In short, if we just want to only compare values use == or if need to compare the value and datatype both use ===.

Thank you for reading! Hope you liked it and learned something from it.