angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

isNumber(NaN) returns true

Open narisipalli opened this issue 14 years ago • 13 comments

NaN is supposed to be Not a number and angular.isNumber(NaN) returns true.

narisipalli avatar Dec 28 '11 23:12 narisipalli

right, but:

$ node
> typeof NaN
'number'

It kind of makes sense if you squint with both eyes and plug your ears.

If you deliberately use NaN in your app, you should use isNaN instead of angular.isNumber.

I'm inclined to say that the current behavior, even though a bit surprising, is consistent with how NaN is being treated in javascript. If you have some good arguments for changing the behavior please share them with us.

IgorMinar avatar Dec 28 '11 23:12 IgorMinar

+1 this is consistent behavior with JS

mhevery avatar Dec 29 '11 03:12 mhevery

Watch out for isNaN too, isNaN("foo") is true. So you want angular.isNumber(x) && isNaN(x)

esprehn avatar Dec 29 '11 08:12 esprehn

typeof NaN

The above expression returns 'number'. But should we consider most common scenarios where 'angular.isNumber' will be called and the expected behaviour?. I would say its redundant to check for NaN most of the time.

tacticiankerala avatar Oct 01 '13 08:10 tacticiankerala

I was expecting similar results to jQuery's jQuery.isNumeric(). NaN being a number just seems silly and though it is consistent behavior with JS, having to !isNaN(num) && angular.isNumber(num) saddens me. The documentation should probably mention that it is only comparing the typeof and reinforcing that NaN is of type "number"; returning true.

TheSharpieOne avatar May 15 '14 23:05 TheSharpieOne

consistency aside, isn't it a logical paradox that something can be both a number and not a number?

if (!isNaN(num) && angular.isNumber(num)) { /* robots' heads explode */ }

Like @TheSharpieOne I also expected the same behavior as jQuery.isNumeric().

androberts avatar Oct 06 '14 17:10 androberts

typeof NaN === "number" --- maybe you want to complain to TC39, although they've definitely heard all of these complaints :p

caitp avatar Oct 06 '14 17:10 caitp

(For the benefit of others reading this -- maybe a call to Number.isFinite() is more appropriate if you want NaNs and Infinities to return false: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite )

androberts avatar Oct 06 '14 20:10 androberts

Angular checks for not null in isObject for obvious logical reasons. Even when typeof null === 'object'. This shows that consistency with JS is not the priority but common sense. So isNumber should check for not NaN too.

miguel-leon avatar Feb 12 '16 17:02 miguel-leon

+1

keithics avatar Apr 02 '16 19:04 keithics

If I wanted to use typeof instead of isNumber I would do so, but isNumber(NotaNumber) === true does not make any sense.

B1aZer avatar Oct 01 '16 13:10 B1aZer

you can rewrite function angular.isNumber

I make this code angular.isNumber = function (num) { var n = Number(num); if (typeof (n) === 'number' && !isNaN(n)) { return true; } return false; };

buster95 avatar Apr 16 '17 02:04 buster95

So today I learned that "Not A Number is number"! Damn it!

alirezamirian avatar Jun 25 '17 12:06 alirezamirian