Michele Nasti

Thoughts on what I learn

Other thoughts about NaN (then, stop)

When I posted the NaN article on facebook in a programmers group, I received some very bad reviews. Let's analyze them.

"It is obvious!"

It is obvious, but it's not common. Partly because I am a developer with some years of experience and, altough I have studied and studying a lot of programming, I have never occourred in NaN-at-work. You might think that this is impossible, but well, it isn't because happened to me. Maybe because I rarely do maths without checking types? Maybe because I rarely do maths in Javascript (it's a very bad idea)?

Why NaN === NaN is false

The fact that NaN !== NaN is true is obvious, after you reason about it.

When I thought of this the first time, I thought it's absurd, but after a while I realized that this is the only possible logic behaviour. As some have said, if I divide a number by a string I get NaN, and if I divide another number by another string I get another NaN. Are these twoNaN equal? no. NaN is not even a measurable property.NaN is a state, and as such it cannot be compared to otherNaNs.

More on this: What is the rationale for all comparisons returning false for IEEE754 NaN values?

Why this is not a JS-only matter

NaN has not been invented by Javascript, or by Java. It is here with us since the first processor, and it is used for a wide range of cases. Here are some:

  1. Division of zero by zero

  2. Dividing an infinity by an infinity

  3. Multiplication of an infinity by a zero

  4. Any operation in which NaN is an operand

  5. Converting a non-numeric string or undefined into a number

It is described in standard IEEE 754, from 1985. In this standard you can read how integer and decimal numbers are stored inside a computer (spoiler: decimal numbers in a computer are not real, continuous, neither infinite). You'll read of many edge cases regarding decimal numbers, along with what should happen when you're in one of the cases described before. So, NaN is a convention followed by pretty much every programming language.

Javascript Quirks

But JS does some stranger things.

  • It has two isNaN() functions, and you must choose the one you need carefully.

  • the standard isNaN() function does type cohercion, that's why isNaN(null) == false.

And Now?

Should every developer in the world know this? Yes, but chances are that you'll never need to use this bit of information.

Is this a good question in a job interview? Well, I don't think so.

Chances are that you'll never need to do this, and even if you hit NaN, that's something you'll learn in two hours wikipedia's reading (or my blog, hehehe).

In a Job interview I would focus on what the candidate can actially do and his way of reasoning.

Of course, if I'm interviewing for a position that has to deal with financial or mathematical programming, knowing of Infinity or NaN (and how numbers are processed by the processor). But I wouldn't do this in Javascript, because JS has no integers.

Is it dumb to explore these areas of a programming language?

In my opinion, no. It's just fun, and a new way to learn something.

In some very bad comments (ones that started with "you are idiot") there were some insightful hints that made me follow links, read articles, study more - and write this.

But on the web I have received some critics like "buy a book and read it", and the first thing I thought is ... haters' gonna hate.

Others have also accused me of "plagiarism" :D

Honestly, this happens in every group that has more than 1-2 thousand members; people will comment out just to say that you're dumb and they're better.