Javascript

Javascript : Difference between == and ===

Posted on

JavaScript has both strict and type-converting equality comparison.

  • 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 objects are strictly equal if they refer to the same Object.

 

Lets take an example – 

if(0 == false ) {

console.log(“inside if”);

}

Here the type conversion happens first and then comparison is done , Hence the condition is true and inside if will be printed on the browser console .

Now ,

if(0 === false ) {

console.log(“inside if”);

}

Here type conversion does not happen , Hence the if condition is false , thread will not go inside if condition .

 

Other Examples in same way –

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