Comparing Things in JavaScript
Different comparison tools available in JS and implementation of Object.is
Hello, beautiful people on the internet.
If you’ve ever scratched your head wondering how comparisons like ==, ===, or Object.is work in JavaScript, you’re not alone.
They’re all about checking if two things are “equal,” but they don’t always behave the same way.
Let’s break it down in simple terms and even create our version of Object.is.
The Tools JavaScript Gives Us to Compare Stuff
1. == (Loose Equality)
This is the chill guy who tries to make things work even if the types don’t match. It says,
“Hey, I’ll convert one value to match the other and then compare.”
Example:
console.log(1 == '1'); // true (number gets converted to string or vice versa)
console.log(false == 0); // true (false becomes 0)
console.log(null == undefined); // true (they're kinda similar in this context)Why it’s confusing:
console.log('0' == false); // true (wait, what?!)It tries too hard and sometimes gives unexpected results.
2. === (Strict Equality)
This is the no-nonsense guy. It says,
“I’m not doing any type conversions. If the type or value doesn’t match, it’s false.”
Example:
console.log(1 === '1'); // false (different types)
console.log(false === 0); // false (no type conversion here)Why it’s better:
It keeps things straightforward, but it’s not perfect.
Get Deep Shah’s stories in your inbox
Join Medium for free to get updates from this writer.
There are a few quirks it doesn’t handle well, like NaN or +0 vs. -0.
3. Object.is
This is the perfectionist. It’s like === but fixes those quirks.
- It knows that
NaNis equal to itself. - It can tell the difference between
+0and-0.
Example:
console.log(Object.is(NaN, NaN)); // true (finally!)
console.log(Object.is(+0, -0)); // false (it knows they’re not the same)That’s where Object.is comes in handy.
Let’s Build Our Own Object.is
Here’s the plan:
- Fix
+0and-0: Check their behavior using1/value. - Handle
NaN: UseNumber.isNaNto check if both values areNaN. - Fallback: For everything else, just use
===.
Here’s how it looks in the code:
const objectIs = (value1, value2) => {
// Handle +0 and -0
if (value1 === 0 && value2 === 0) {
// divison of 1 with +0 gives Infinity
// divison of 1 with -0 gives -Infinity
// So we are comparing Infinity & -Infinity
return 1 / value1 === 1 / value2;
}
// Handle NaN
if (Number.isNaN(value1) && Number.isNaN(value2)) {
return true;
}
// Fallback to strict equality
return value1 === value2;
};Let’s Test It Out
Special Cases:
console.log(objectIs(+0, -0)); // false
console.log(objectIs(-0, -0)); // true
console.log(objectIs(+0, +0)); // true
console.log(objectIs(NaN, NaN)); // trueNormal Stuff:
console.log(objectIs(42, 42)); // true
console.log(objectIs('hello', 'hello')); // true
console.log(objectIs(null, null)); // true
console.log(objectIs(undefined, undefined)); // trueObjects and Arrays:
console.log(objectIs([], [])); // false (different memory references)
console.log(objectIs({}, {})); // false (different memory references)
console.log(objectIs([1], [1])); // falseSymbols:
console.log(objectIs(Symbol('a'), Symbol('a'))); // false (unique symbols)
console.log(objectIs(Symbol.for('a'), Symbol.for('a'))); // true (shared global symbol)When to Use What?
- Use
==if you need type conversion (but honestly, avoid it). - Use
===for most comparisons—it’s simple and reliable. - Use
Object.iswhen you care about edge cases likeNaNor+0/-0.
I’m always looking to improve and would love to hear your thoughts! If you have any suggestions, feedback, or topics you’d like me to cover, please feel free to share them in the comments below. Your insights are greatly appreciated! 🙂
Thank you for being a part of the community
Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Newsletter | Podcast
- Check out CoFeed, the smart way to stay up-to-date with the latest in tech 🧪
- Start your own free AI-powered blog on Differ 🚀
- Join our content creators community on Discord 🧑🏻💻
- For more content, visit plainenglish.io + stackademic.com









