Learnitweb

What is the difference between == and === operator?

JavaScript has both strict and type–converting comparisons. The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. The identity operator returns true if the operands are strictly equal with no type conversion.

For strict equality the objects being compared must have the same type and:

  • 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.
  • (Null==Undefined) is true but (Null===Undefined) is false.
console.log(1 == 1);     // true 
console.log(1 == '1');   // true, auto type coercion, string converted into number
console.log(0 == false); // true because false is equivalent to zero
console.log(0 == null);  // false
var object1 = {'key': 'value'}, object2 = {'key': 'value'}; 
console.log(object1 == object2); // false
console.log(object1.key == object2.key);  //true
console.log(object1 === object2);  // false
console.log(0 == undefined);  //false
console.log(null == undefined);  // true
console.log(1 === 1);   //true
console.log(1 === '1') // false as type of these are not equal