DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Helder B. Berto
Helder B. Berto

Posted on • Originally published at linkedin.com

๐Ÿ”ฅ Quick Tip: How to compare objects more accurately

On this simple trick I'll show you how to compare objects more accurately using Object.is().

console.log(Object.is(0, '')); // => false
console.log(Object.is(null, undefined)); // => false
console.log(Object.is(undefined, undefined)); // => true
console.log(Object.is([1], true)); // => false
console.log(Object.is(NaN, NaN)); // => true

const obj1 = { name: 'Helder' };
const obj2 = { name: 'Helder' };
const referenceObj1 = obj1;

console.log(Object.is(obj1, obj2)); // => false
console.log(Object.is(obj1, obj1)); // => true
console.log(Object.is(referenceObj1, obj1)); // => true
Enter fullscreen mode Exit fullscreen mode

Note: When comparing two objects it will compare the memory point too.

Did you like it? Comment, share! โœจ

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

โญ๏ธ๐ŸŽ€ JavaScript Visualized: Promises & Async/Await

async await