The Object is() method is used to check if two values are the same.
This method provides functionality similar to the strict equality (===) operator, but there are some differences particularly with NaN and +0/- 0.
Two values are the same if one of the following holds:
- Both are undefined
- Both are null
- Both are true, or both are false
- Both strings are of the same length with the same characters in the same order
- Both are the same object (which means both objects have the same reference)
- Both numbers are and
- both +0
- both -0
- both NaN
- both non-zero and both not NaN, and both have the same value
Syntax
Object.is(value1, value2);
Parameters
- value1(required): The first value to compare.
- value2(required): The second value to compare.
Return value
Returns a boolean value indicating whether the two arguments are same or not.
Visual Representation
Example 1: How to Use Object.is() Method
let a = 'AppDividend';
let b = 'AppDividend';
let c = 'Appdividend';
console.log(Object.is(a, b))
console.log(Object.is(a, c))
Output
true
false
Example 2: Zero and NaN comparison
console.log(Object.is(0, -0));
console.log(Object.is(+0, -0));
console.log(Object.is(+0, 0));
console.log(Object.is(-0, -0));
console.log(Object.is(NaN, 0/0));
console.log(Object.is(NaN, NaN));
Output
false
false
true
true
true
true
Example 3: Comparing empty arrays and objects
let arrA = [];
let arrB = [];
console.log(Object.is(arrA, arrB));
let objA = {};
let objB = {};
console.log(Object.is(objA, objB));
Output
false
false
Browser Compatibility
- Chrome 19 and above
- Edge12 and above
- Firefox 22 and above
- Opera 15 and above
- Safari 9 and above
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.