Why does angular.isNumber (NaN) return true?
Quoting IgorMinar , Angular Developer in this exact question:
$ node > typeof NaN 'number'
This makes sense if you are squint with both eyes and plug your ears.
If you are using NaN on purpose in your application, you should use isNaN instead of angular.isNumber.
I'm inclined to say that the current behavior, although a bit surprising, is consistent with how NaN is handled in javascript . If you have a good case for behavior change, please share it with us.
So the question is really about the non-Angular javascript standard itself
And to answer that question we have to go to the ECMAScript 5 type number specification , of course it says:
4.3.20 Room type
a set of all possible values โโof numbers , including the special "Not-a-Number" (NaN) , positive infinity and negative infinity
4.3.23 NaN
a numeric value that is the IEEE 754 Non-Number value
So, according to the latest ECMAScript Specification I am number
source to share
Here is the best way I can think of to explain it.
Although a value NaN
represents something that is not a number, the value NaN
itself is still a type of a number (in the sense of the type system).
It is also the IEEE 754 specific floating point value that JavaScript uses for numbers. It makes sense that infinity and NaN values โโwould be number types.
source to share
the ECMA specification defines NaN as the value of an IEEE 754 Not-a-Number. One of the reasons for NaN
global is numeric comparison. You also need to represent undefined numeric results such as value Math.sqrt(-1)
. So, it is not particularly AngularJS specific. Consider the following:
typeof NaN === "number" // true
typeof NaN === typeof NaN // true
typeof NaN === typeof 123 // true
NaN === NaN // false
isNaN(NaN) // true
isNaN(123) // false
isNaN('123') // false
isNaN('|23') // true
So, isNumber
returns true for NaN
because it is Number
. To check numeric values โโuse isNaN()
.
source to share
Most likely angular is just using the type of what you are passing. If the type is a number, then it returns true.
If you want to know if something is a number (excluding NaN), you can do the following.
function isNumber(val){
return angular.isNumber(val) && (val == val);
}
This works by first determining if val is a number. If it's a test, make sure it's NaN.
NaN is not equal to itself (or any other number for that matter).
source to share
There are really two meanings of "number" here:
- the abbreviation "NaN" means that there is no meaningful answer to a specific mathematical operation; you can say "there is no such number"
- however, every value in a language such as JS has a type, and the data type that goes into and out of mathematical operations is known in JS as "number"; thus, when JS wants a special meaning to say that a math operation has no answer, that special value is a special number
Note that this seeming contradiction is less obvious in other languages, because JS is unusual in using only one numeric type rather than (at least) integer and real / float types. Having NaN as a floating point value is standard in almost all modern languages, but the word "number" makes it seem more surprising in JS.
Angular function is one of many utilities for checking the type of value. The documentation for it mentions that it returns true for infinities and NaN and points to a standard function isFinite
when not desired.
source to share