equality comparison operator in JavaScript

The triple equals operator

The triple equals operator (===) returns true if both operands are of the same type and contain the same value. If comparing different types for equality, the result is false.

This definition of equality is enough for most use cases. When comparing the string "0" and the number 0 the result is false as expected.

The double equals operator

The double equals operator (==) tries to perform type conversion if the types differ and then compare for equality. If the types differ, either or both operands are first converted to a common type. The conversion rules are complex and depend on the argument types.

ToNumber(A) attempts to convert its argument to a number before comparison. Its behavior is equivalent to +A (the unary + operator). ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.

According to ECMAScript, all objects are loosely unequal to undefined and null

var num = 0;  
var obj = new String("0");  
var str = "0";  
var b = false;

console.log(num == num); // true  
console.log(obj == obj); // true  
console.log(str == str); // true

console.log(num == obj); // true  
console.log(num == str); // true  
console.log(obj == str); // true  
console.log(null == undefined); // true

// both false, except in rare cases
console.log(obj == null);  
console.log(obj == undefined);  

Loose equality is one such context: null == A and undefined == A evaluate to true.if, A is an object that emulates undefined. In all other cases an object is never loosely equal to undefined or null.