Skip to content
loading...

🔥 Quick Tip: How to compare objects more accurately

helderburato profile image Helder Burato Berto github logo ・1 min read

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

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

Did you like it? Comment, share! ✨

twitter logo DISCUSS
Discussion
markdown guide
Classic DEV Post from Aug 6 '19

What to put in your portfolio as a beginner web dev

Helder Burato Berto profile image
Front-end Developer @ Cheesecake Labs • Learning, practicing and creating this is my motto 💥