Comparing two or more Javascript Objects using Object.is() Method
Javascript Object.is() method compares two or more objects and returns true if both are same or returns false. The checking is not just limited to type checking or checking of value it goes beyond and checks its instance or the objects are the same.
Syntax
Object.is(object1, object2, object3...);
The Javascript Object.is() method returns boolean value.
var arr = [1,2,3]; var copy = arr; console.log(Object.is(arr,copy)); //Console Output true
Comparing all objects, string, and numbers with Object.is() method.
var str1 = "Javascript"; var str2 = "Javascript"; console.log(Object.is(str1,str2)); // true var num1 = 100.25; var num2 = 100.25; console.log(Object.is(num1,num2)); // true console.log(Object.is(null,null)); // true var arr1 = []; var arr2 = []; console.log(Object.is(arr1,arr2)); // false var arr3 = arr1; console.log(Object.is(arr1,arr3)); // false var obj1 = {}; var obj2 = {}; console.log(Object.is(obj1,obj2)); // false var obj3 = obj1; console.log(Object.is(obj1,obj3)); // true
Comparing multiple objects
var obj1 = {}; var obj2 = {}; var obj3 = obj1; console.log(Object.is(obj1,obj2,obj3)); //Console Output false
We can compare multiple objects and the checking ours in AND
the logic that is to return true
all for the above 3 objects these objects must be the same if one object is different this does not match with other objects and the method returns false
.
Conclusion
So it’s time to say bye we have come to the end of our post on Comparing two or more Javascript Objects using Object.is() Method.If you like our content then please do share it and comment to clarify your doubts.
Related Posts
- JavaScript Array Methods | push(), pop(), shift(), unshift(), indexOf() etc
- Javascript Advanced Object Operations with examples




