Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Comparing Things in JavaScript

3 min readJan 9, 2025

--

Different comparison tools available in JS and implementation of Object.is

Press enter or click to view image in full size

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 NaN is equal to itself.
  • It can tell the difference between +0 and -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:

  1. Fix +0 and -0: Check their behavior using 1/value.
  2. Handle NaN: Use Number.isNaN to check if both values are NaN.
  3. 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)); // true

Normal Stuff:

console.log(objectIs(42, 42)); // true
console.log(objectIs('hello', 'hello')); // true
console.log(objectIs(null, null)); // true
console.log(objectIs(undefined, undefined)); // true

Objects and Arrays:

console.log(objectIs([], [])); // false (different memory references)
console.log(objectIs({}, {})); // false (different memory references)
console.log(objectIs([1], [1])); // false

Symbols:

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.is when you care about edge cases like NaN or +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:

--

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.