The Object.is() method in vanilla JS

Today, we’re going to look at the Object.is() method: what it does, how it works, and when you might want to use it. Let’s dig in.
What the Object.is() method does The Object.is() method checks if two values are the same, and returns a boolean: true if they’re the same, and false if they’re not.
You pass the two values in as arguments.
// returns true Object.is(42, 42); // returns false Object.


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to look at the Object.is() method: what it does, how it works, and when you might want to use it. Let’s dig in.

What the Object.is() method does

The Object.is() method checks if two values are the same, and returns a boolean: true if they’re the same, and false if they’re not.

You pass the two values in as arguments.

// returns true
Object.is(42, 42);

// returns false
Object.is('orange', 'blue');

How the Object.is() method works

It works mostly like the strict equals operator, but with a few small differences we’ll get into in just a bit.

Just like with strict equals, it does not check if two arrays or objects have the same value. It checks if they’re the same object.

let wizards = ['Gandalf', 'Radagast', 'Merlin'];
let otherWizards = wizards;

// Since otherWizards is just a reference to wizards
// returns true
Object.is(wizards, otherWizards);

// Because we've created an immutable copy
// returns false
Object.is(wizards, [...wizards]);

For strings and numbers, which are always immutable, it checks the value.

let num1 = 42;
let num2 = 42;

// returns true
Object.is(num1, num2);

One notable way that it differs from strict equals is in how it handles NaN and signed zeroes (a 0 with a + or - symbol).

// returns false
let n = NaN === NaN;

// returns true
Object.is(NaN, NaN);

// returns true
let z = 0 === -0;

// returns false
Object.is(0, -0);

Yea, JavaScript is weird!

When should you use this?

I see myself continuing to use strict equals for the foreseeable future, simply because it’s fewer characters and muscle memory for me to type.

When doing a comparison where NaN or a signed zero might be one of the values and you want to the behavior described above, however, Object.is() might be a better choice. That feels very situational to me, though.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-03-04T15:30:00+00:00) The Object.is() method in vanilla JS. Retrieved from https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/

MLA
" » The Object.is() method in vanilla JS." Go Make Things | Sciencx - Thursday March 4, 2021, https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/
HARVARD
Go Make Things | Sciencx Thursday March 4, 2021 » The Object.is() method in vanilla JS., viewed ,<https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » The Object.is() method in vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/
CHICAGO
" » The Object.is() method in vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/
IEEE
" » The Object.is() method in vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/. [Accessed: ]
rf:citation
» The Object.is() method in vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/03/04/the-object-is-method-in-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.