JavaScript

JavaScript Object.is() method | Explained

In JavaScript, the “Object.is()” method is primarily used to validate the equality of two values through comparison. These values can be string, float, decimal, or integer type. Moreover, the “Object.is()” method also provides the functionality to check the polarity of two numbers.

Do not confuse the “Object.is()” method with the JavaScript equality “==” operator because there exist significant differences between them. For instance, the “Object.is()” is a JavaScript method that is utilized for performing comparison based on “original” values of primitive data types, and the “==” operator compares specified values after converting them to a common type. Secondly, the equality operator marks the numbers “-1” and “1” as equal, whereas the Object.is() method treats them according to their polarity.

This write-up will explain the Object.is() method and its usage in JavaScript. So, let’s start!

How to use JavaScript Object.is() method

JavaScript Object.is() method is invoked in the following use cases:

  • To compare two numbers.
  • To compare two strings.
  • To compare two objects.
  • To compare the polarity of two numbers.

We will discuss the mentioned use cases of the Object.is() method in the next section, but before jumping into it, check out the syntax of the Object.is() method in JavaScript.

Syntax of using JavaScript Object.is() method

Object.is(value1, value)

Here, “value1” represents the value that needs to be compared to “value2”:

How to use JavaScript Object.is() method for comparing strings

The JavaScript “Object.is()” method can be used for comparing strings. For this purpose, you have to pass both strings as arguments to the Object.is() method in the following way:

console.log(Object.is("linuxhint", "linuxhint"));

The above-given “Object.is()” method will compare the first “linuxhint” string to the second “linuxhint” string, in terms of “length”, “characters,” and the “order” in which characters are assembled.

In our case, both values are equal according to the mentioned criteria, so the return case of the “Object.is()” method will be set to “true”:

Now, let’s change the second argument value to “linux” and check out the output of the Object.is() method:

console.log(Object.is("linuxhint", "linux"));

As the length of the specified string arguments is not the same, Object.is() method will not further compare them and returns a “false” value:

How to use JavaScript Object.is() method for comparing objects

In your program, you can also utilize “Object.is()” method for to perform a comparison between objects. For instance, the below-given Object.is() method will compare two empty objects:

console.log(Object.is({}, {}));

Output

At this point, you must be questioning yourself that the passed objects are empty, neither of them has any key-value pair, then why “Object.is()” returned “false”?

The specified objects look the same; however, they are two different objects as they refer to different memory addresses. This is why the “Object.is()” method marked these empty objects as unequal after comparing their references.

Also, it does not matter if the added “key-value” pairs are the same in both objects. The JavaScript “Object.is()” method will still return “false” after execution :

let object1 = { age: 23 };

let object2 = { age: 23 };

console.log(Object.is(object1, object2));

Output

Two objects are only considered “equal” if they point towards the same memory address. For example, when we will compare the created “object1” to itself, the “Object.is()” set “true” as its return case:

console.log(Object.is(object1, object1));

Output

How to use JavaScript Object.is() method for comparing polarity of two numbers

The polarity of a number signifies whether the number is positive or negative. Want to compare the polarity of two numbers in JavaScript? Utilize the “Object.is()” method in your code and specify numbers as arguments in it.

For instance, the numbers “-1” and “1” passed to the Object.is() method are not equal as “-1” is smaller than “1”, so the “Objects.is()” method will return “false”:

console.log(Object.is(-1, 1));

Output

If the specified numbers are equal in terms of polarity, then in the next step, the “Object.is()” method will compare them and return “true” if their values are equal.

For instance, both of the arguments in the below-given “Object.is()” method are “positive,” and their values are equal, so the resultant boolean value will be printed out as “true”:

console.log(Object.is(3, 3));

Output

We have compiled all of the essential information related to the JavaScript Object.is() method. You can further explore it according to your requirements.

Conclusion

JavaScript Object.is() method is used to compare two values. It treats objects and primitive values differently. In the case of primitive values, Object.is() method check them “by-value” and compares their “length”, “characters,” and the “order of characters”, whereas “objects” are compared based on “references”. The polarity of multiple numbers can also be compared using JavaScript.is() method. This write-up explained the working of the JavaScript Object.is() method.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.