avatarDr. Derek Austin 🥳

Summary

The provided web content discusses the differences between the strict equality operator === and the Object.is() method in JavaScript, highlighting two key distinctions: the treatment of negative zero and NaN values.

Abstract

The web content introduces the Object.is() method, a new addition in ES6 (ECMAScript 2015) that provides a nuanced alternative to the strict equality operator === in JavaScript. Unlike ===, Object.is() differentiates between negative and positive zero, considering them unequal. It also uniquely treats NaN (Not-a-Number) as equal to itself, which is contrary to the behavior of ===. This method is particularly relevant in modern JavaScript frameworks like React, where it is used to prevent unnecessary re-renders by accurately comparing state values, including those that are NaN. The article emphasizes that while Object.is() behaves similarly to === for most values, its specialized handling of edge cases makes it a valuable tool for developers.

Opinions

  • The author suggests that developers should be aware of the Object.is() method, as it can appear in modern JavaScript codebases, such as those using React.
  • The use of Object.is() in React is seen as beneficial, as it allows React to optimize component re-rendering by correctly identifying when state values have not changed, even when dealing with NaN values.
  • The article implies that the Object.is() method is a superior choice for equality comparison in JavaScript due to its consistent behavior with edge cases like -0 and NaN.
  • The author provides real-world examples and references to other works, suggesting that understanding Object.is() is crucial for JavaScript developers who want to write robust and efficient code.
  • There is an emphasis on the importance of knowing the nuances between Object.is() and ===, as well as the differences between loose (==) and strict (===) equality operators in JavaScript.
  • The author encourages the transpilation of Object.is() for compatibility with older browsers, indicating a forward-thinking approach to JavaScript development that embraces new features while maintaining backward compatibility.

Object.is(+0,-0) is true, Object.is(NaN,NaN) is true; compare to ===

ES6: Object.is() vs. === in JavaScript

ES6 (ECMAScript 2015) added a helper function called Object.is() that is slightly different from the === operator.

Photo by Alexey Topolyanskiy on Unsplash

The triple equality operator === is the trusty stand-by for testing equality in JavaScript, being dutifully used daily by developers everywhere.

In fact, === has been the preferred method of equality comparison over its type-coercing friend == since the very beginning, JavaScript 1.0! 💯

ECMAScript 6th Edition (ES6, or ES2015) added an additional comparison operator that has slightly different behavior compared to ===.

That new method is called Object.is() and is something you should know about, in case it pops up in your JavaScript, like it does in React! 😁

Photo by Nathan Anderson on Unsplash

The 2 differences between === and Object.is()

“[The] Object.is() method is used to determine whether two values are the same or not. […] The == and === operator treats the number values +0 and -0 as equal whereas Object.is() method treats them as not equal. Apart from that the == and === operator does not treat Number.Nan [sic] equal to Nan [sic].” — Geeks for Geeks

Let’s put that in code, shall we?

1 — Negative Zero

The unusual JavaScript number negative zero >-0 is usually considered equal to positive zero +0, but they are technically different values.

Object.is() can tell them apart, returning false for the comparison between -0 and +0, in case that ever comes up:

2 — NaN

NaN represents the value “not a number” and equivalently represented by NaN or Number.NaN.

NaN is the only number that does not equal itself in JavaScript, but NaN is considered to be the same value as itself using Object.is():

Photo by Nathan Anderson on Unsplash

Object.is(+0,0) is false, Object.is(NaN,NaN) is true

So Object.is() is just === with different behavior for negative zero -0 and NaN — good to know!

As an ES6 feature, it may not be supported by older browsers, but it can be transpiled using Babel for compatibility.

In the next section, I compare Object.is() to the == operator then speculate why React uses Object.is() under the hood.

Photo by Ales Krivec on Unsplash

The difference between == and ===

The double and triple equality operators behave differently, so it is important to note that Object.is() behaves like ===, the strict equality operator, not like == , the loose equality operator.

“Remember, the main difference between == and === operators is that == compares variables but performs type coercion. But === doesn’t do that. It checks not only the values but also types of two variables.”

gravity well in JavaScript in Plain English

So that means that Object.is() will not perform type coercion.

Confused? Try my article on the differences between == and ===:

Photo by Pablo Heimplatz on Unsplash

A real world example of Object.is()

React developers know that one of the key features of React is that the component is re-rendered when the state is updated.

Here’s how that works, using React Hooks or a class component:

“The setCount method returned by the useState hook works similar to this.setState in the class component. They both pass the updated state to react and enqueue a re-render.

However, there is a difference — In case of setState, React will always re-render the component and it’s children(unless the shouldComponentUpdate lifecycle method returns false). With setCount, React will bail out on an update if the updated state passed is same as previous state. React uses Object.is algorithm to compare the state.”

Avinash Ega in The Startup

Note the use of Object.is(). This potentially keeps React from infinitely re-rendering a component when given a NaN value, which would never equal itself using ===, as I explored in my article on checking for NaN:

Photo by Florian van Duyn on Unsplash

Conclusion

The method Object.is() works the same as === for almost all intents and purposes, with just two key differences.

The first difference is that Object.is() thinks -0 and +0 are different values, while === says they are the same value.

The second difference between is that Object.is thinks NaN is the same value as itself, while === says they are not same value.

React is an example of Object.is() being used in the wild. Happy coding!

Photo by sayid he on Unsplash

Further reading:

Photo by Mickey O'neil on Unsplash

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Web Development
Coding
Software Development
Recommended from ReadMedium