Comparing NaN and -0 With Object.is()

With the old page’s labels fixed, the next line of the readings check marks a temperature that hasn’t changed. Mill sent 7.4°C twice, and orchard’s failed thermometer sent nan both times. In Python, you need the same_temp() helper for orchard because nan == nan is False. JavaScript builds that helper in, and it disagrees with === once more, in a way the website can show.

Replace the Helper With Object.is()

NaN === NaN is false, as in Python, so a comparison with === NaN never evaluates to true, and Number.isNaN() is how you find a lone NaN. The static method Object.is(a, b) asks whether two values are the same value, treating NaN as equal to itself, so it takes the place of Python’s same_temp(reading["temp"], reading["last_temp"]):

Language: JavaScript
const reading = { station: "orchard", temp: NaN, lastTemp: NaN };

const unchanged = Object.is(reading.temp, reading.lastTemp);
console.log(reading.temp === reading.lastTemp, unchanged);

=== calls orchard’s readings different, and Object.is() agrees with the helper:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.