DEV Community

Cover image for JavaScript comparison that never lies
Sergey Panarin
Sergey Panarin

Posted on

JavaScript comparison that never lies

Is there something not equal to itself?

One of the popular JavaScript questions among the interviewers:

Could x !== x?

It refers to one of the most unknown parts of JavaScript – types, coercion, and its corner cases.

In the example above for NaN even triple equal comparison lies. And is there some way to be sure what's inside the variable?

Function that never lies

Yes, there is – it's Object.is(x, y).

It tells the truth in all the cases, even for -0 or NaN.

// Object.is(x, y) never lies

console.log(Object.is(true,true) === true);
console.log(Object.is(false,false) === true);
console.log(Object.is(null,null) === true);
console.log(Object.is(undefined,undefined) === true);
console.log(Object.is(NaN,NaN) === true);
console.log(Object.is(-0,-0) === true);
console.log(Object.is(0,0) === true);
Enter fullscreen mode Exit fullscreen mode

Use it in your interview answers and in your code – Object.is never lies!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay