What Is Object.is() in Java Script?

Introduction: What Is Object.is() in Java Script?

About: I ain't no physicist, but I knows what matters. :p By the way, I'm an Electronic Engineer . Interested in Electronic Hobby's and DIY Projects . I'm Focusing mostly on Simulations of Projects and Further more …

In this post you’ll learn how to use the new ES6 method Object.is() in JavaScript.

What isObject.is()?
Introduced in ECMAScript 2015, known as ES2015 or ES6, Object.is() allows us to test equality of JavaScript objects (and primitive values, despite the name).
Object.is() is very similar to the JavaScript strict comparison operator === and can almost be used interchangeably (despite some minor differences which we’ll cover).
We won’t discuss the differences between both == and === here, as I’ll assume you know what a strict comparison with === does. A strict comparison performs no type conversion, known as coercion, compared to non-strict (behind the scenes they both perform type coercion, but strict disregards it when evaluating equality).

Step 1:

In its simplest form, we could use Object.is() with strings, numbers or objects (and much more) as shown above:

Step 2:

We’ll momentarily explore the differences between Object.is() and === as well, as here they act the same!

When using ===, we are performing a value-comparison operation. When using Object.is() we are performing a check using the SameValue algorithm, which looks like so as shown above:

Step 3: Why Should I Use Object.is() Over ===?

Really, we’re here to answer one question -

Differences between Object.is() and === There are two major differences between Object.is() and the triple equals comparison ===. All other operations with Object.is() and === will produce the identical result.
The first difference is that -0 and +0 can now be properly compared as shown above:

Step 4:

rally, it comes down to NaN (Not-a-Number) and how it fixes the behavior when comparing values that could be NaN - such as Number('abc') as shown above:

Step 5:

This tells us that Object.is() was also created to help us to properly distinguish between NaN values (and even the newly added Number.NaN).

We also get the added benefit with the Object.is() prototype method that it follows a more functional programming style.

Other Array prototype methods such as Array ForEach and Array Reduce also slot nicely into a functional programming style. However, I will admit the code is likely cleaner using ===, so use what makes sense.
Check out the examples I’ve created bellow

console.log(+0 === -0); // true

console.log(Object.is(+0, -0)); // false;

console.log(NaN === NaN); // false console.log(Object.is(NaN, NaN)); // true

console.log(Number.NaN === Number.NaN); // false console.log(Object.is(Number.NaN, Number.NaN)); // true

console.log(NaN === Number.NaN); // false console.log(Object.is(NaN, Number.NaN)); // true

Summary
We’ve covered the new ES6 Object.is() method and compared it to JavaScript’s strict triple equals statement ===.

Learning the differences between the two, we can now choose to use Object.is() as a better comparison check for NaN values, as well as adopting a more functional programming style.

Happy coding!

Be the First to Share

    Recommendations

    • Maps Challenge

      Maps Challenge
    • Fandom Contest

      Fandom Contest
    • Metal Contest

      Metal Contest

    Comments