Feature Request: Number.isNumber()
El 10/4/2016 5:08 PM, "Adrian Sieber" <adrian.sieber1 at gmail.com> escribió:
Hi everybody,
checking if a value is a number is currently really complicated and I have stumbled upon a fair amount of code where this lead to bugs.
The easiest way is
typeof value === 'number'
.
What about Number.isFinite?
This however has the drawback that
NaN
also evaluates to true.
Number.isFinite(NaN) is false
Furthermore, basically every expression preceded by a
+
evaluates to
true.
… and there are even more gotchas. For a full roundup read
This implementation could also be used as a starting point. (However, I would argue that
isNumber('3')
should evaluate to false. It's a string after all.)Therefore I think a
isNumber
functionality should already be provided
natively.
What about Number.isFinite?
Thanks for the heads up. I wasn't aware that there was a fixed version of
isFinite
added to ES2015.
The global one was just a mess , but the new one seems to do
exactly what I was looking for. Problem solved =). Thanks a lot!
checking if a value is a number is currently really complicated and I have stumbled upon a fair amount of code where this lead to bugs.
The easiest way is
typeof value === 'number'
. This however has the drawback thatNaN
also evaluates to true. Furthermore, basically every expression preceded by a+
evaluates to true. … and there are even more gotchas. For a full roundup read jonschlinkert/is-number.This implementation could also be used as a starting point. (However, I would argue that
isNumber('3')
should evaluate to false. It's a string after all.)Therefore I think a
isNumber
functionality should already be provided natively. What do you think?