Hacker News new | ask | show | jobs
by xupybd 4367 days ago
So it's Not-a-Number but isNumber returns true?

That would imply then that isNumber is named incorrectly. So perhaps isNumber should read isNumericType?

Most of the time I want to test, is this a number I can use in a normal mathematical sense. As in, can I use it in subtraction, multiplication etc. I know now that I should use isFinite and isNumber, but wouldn't it be nice to have just one well named function for this?

2 comments

> So it's Not-a-Number but isNumber returns true?

NaN is not a number (the concept) but it is a Number (the type).

> That would imply then that isNumber is named incorrectly.

Arguably, it is Number (the type) that is named incorrectly (a type called Number that includes a value called Not-a-Number seems problematic.) Maybe "Number" should be called "MaybeNumber".

And, perhaps more importantly, the fact that JS doesn't have a convenient type membership operator for primitive types is problematic, as if there was a convenient parallel to "instanceOf" for primitive types -- or if "instanceOf" just worked for them -- then libraries wouldn't need to provide something like what underscores isNumber does at all, and then any isNumber function would probably test "is the argument a number" rather than "is the argument a Number".

We just need a way to indicate if the method is checking the type or the value.
It's consistent with the names of other type-checking functions. isNumber checks if a value is an instance of the Number class just like isDate checks if a value is an instance of the Date class. If it were isNumericType, they'd get questions about why the function doesn't follow the existing naming convention.

The only reason this is confusing for people is because they aren't familiar with IEEE 754 floating-point numbers and their representations. But since floats are used in almost every programming language, it's something that any programmer should be familiar with. Once they learn exactly what NaN means in that context, then the confusion should disappear.

> The only reason this is confusing for people is because they aren't familiar with IEEE 754 floating-point numbers and their representations.

NaN is part of IEEE 754, but it is not a number.

The reason people are confused is because the JS datatype name has a small but critical disconnect from what the values it contains are.

I really dislike this sort of reasoning. That its only confusing because you don't know enough. It's possible to word things in ways that are easy to learn and lead you to an understanding of the underlying mechanics.

Its confusing because NaN explicitly states not a number. Then the function explicitly states is number.

However the two are in very different contexts, yet this is not communicated when using them. It's intuitive to think of a date as a class and there for the context is implicit in isDate. I don't think this is the case for isNumber.

I think this is less about people understanding floating-point numbers more about understanding that a function starting with is implies some form of type checking.

> Its confusing because NaN explicitly states not a number. Then the function explicitly states is number.

The problem is that "Number" is the name of a JS data type, and "number" is the name of a concept, the two don't match exactly, and when you are using camel-casing for word separation, you can't distinguish the two.

The function "isNumber" checks if the thing it is applied to is a JS Number, not if it is a number.