Advance comparison

Javascript has several ways of equality check ==, === and the Object.is() method.

Unlike ==, === does not convert the types of the operands.

Unlike == and ===, Object.is() does not distinguish +0 and -0. Also does not distinguish Number.NaN and global NaN.

// comparing two literal of different type    
console.log("2=='2' is ", 2=='2');  // true
console.log("'2'==2 is ", '2'==2);  // true 
console.log("'2'===2 is ", '2'===2);  // false
// comparing two objects var obj1 = {foo : "foo", bar: "bar", num:23}; var obj2 = {foo : "foo", bar: "bar", num:23}; console.log("obj1==obj2 is ", obj1==obj2); // false, although same fields and values console.log("obj1===obj2 is ", obj1===obj2); // false console.log("Object.is(obj1,obj2)", Object.is(obj1,obj2)); // false // convert objects to string literals and compare // be careful, that fields have same order console.log("compare via == of JSON.stringify ", JSON.stringify(obj1)==JSON.stringify(obj2)); // true // true, strict comapring two string literal console.log("compare via === of JSON.stringify ", JSON.stringify(obj1)===JSON.stringify(obj2)); // true // false, strict comparing of console.log("compare via === of JSON.stringify ", JSON.stringify(obj1)===new String(JSON.stringify(obj2))); var obj3 = {foo : "foo3", bar: "bar3", num: 69}; var obj4 = {}; Object.assign(obj4, obj1); console.log("compare after first assign ", JSON.stringify(obj4)==JSON.stringify(obj1)); // true // here we assign other object with same fields, // but with other values Object.assign(obj4, obj3); console.log("compare after second assign ", JSON.stringify(obj4)==JSON.stringify(obj3) ); // true

comparison undefined, null and ""

console.log("null and null: ", 
            null==null, // true 
            null===null,// true
            Object.is(null, null), // true
            JSON.stringify(null)==='null' // true
           );  
   
console.log("undefined and undefined: ", 
            undefined==undefined,  // true 
            undefined===undefined, // true
            'undefined'==undefined,// false
            Object.is(undefined, undefined), // true
            JSON.stringify(undefined)==='undefined' // false
           );     

"", null, undefined are treated as false values in conditional expressions.

console.log("'' and false", 
            ""==false, // true 
            ""===false); // false
    
console.log(null ? true: false);  // false
console.log(undefined ? true : false); // false
console.log("" ? true : false); // false 

shallow comparison of objects

For shallow comparison of objects you can use Object.entries() or JSON.stringify() methods.

Unlike other comparison methods, following method return false if any operand is null or undefined.

var obj1 = {foo : "foo", bar: "bar", num:23};
var obj2 = {foo : "foo", bar: "bar", num:23};
var obj3 = {foo : "foo3", bar: "bar3", num: 69};
var obj4 = {};
Object.assign(obj4, obj1);  

Object.isObjEqual = function(a, b) {

    if (a !== undefined && a !== null 
        && b !== undefined && b !== null) {
        return JSON.stringify(a) == JSON.stringify(b);
    }

    return false;
}    
    
console.log("isObjEqual: ", 
           Object.isObjEqual(obj4, obj3), // false
           Object.isObjEqual(obj2, obj1), // true
           Object.isObjEqual(obj1, obj4));// true