Javascript Numerics

I ran into an interesting issue very recently. I needed a way to check if a string was a number. Piece of cake.  Skip to the bottom for the solution.

>> parseInt(“10”)
> 10

So far so good.

>> parseInt(“hurr”);
> NaN

Excellent. With a little Underscore JS action…

>> !_.isNaN(parseInt(“w”)
> false

Cool.

>> parseInt(“w10”)
> NaN

Stupendous.

>> parseInt(“10a”)
> 10

Wait what?

>> parseInt(“10b”)
> 10

Uhh. Must be interpreting the ‘a’ or ‘b’ as hex?

>> parseInt(“10w”)
> 10

Fuck.

Well what about Underscore’s internal _isNumber?

>> _.isNumber(“10k”);
> false

HAH! Great. Let’s check the base case…

>> _.isNumber(“10”);
> false

Damnit.

Well, after a great deal of deliberation, I threw together the following effective, if gross method:

_.reduce(x, function(memo, val) {return memo && !_.isNaN(parseInt(val))}, true);

Or, wrapped more nicely:

isNum = function(x) {return _.reduce(x, function(memo, val) {return memo && !_.isNaN(parseInt(val))}, true)}

>> isNum(“10a”)
> false

>> isNum(“10”)
> true

>> isNum(10)
> true

Addendum: This method may plausibly be faster, considering Underscore defers to the native every when available, and allows for early-out:

isNum = function(x){ return _.every(x, function(y){ return !_.isNaN(parseInt(y)); }); }

Comments are closed.