4 ms·
isNumber would make for a legit package. Is there a good way in javascript to determine confidently if an arbitrary input is a number? e.g: [1, "one", true, fa
by newsbinator 5y ago
isNumber would make for a legit package. Is there a good way in javascript to determine confidently if an arbitrary input is a number?
e.g: [1, "one", true, false, 0, "zero", null, undefined]
edit: also ["1", "0", etc]
edit 2: also [NaN]
- Izkata 5y ago“number" == typeof variable Works for decimals, too. Although maybe you meant to include "1" In your examples?
- madeofpalk 5y agoUnfortunately "not a number" is typeof "number" "number" === typeof NaN Returns true.
- organsnyder 5y agoThat's a tricky one that I think would depend on use case. Yes, of course NaN isn't a number, but it's still kind of a number, depending on context.
- ohgodplsno 5y agoNaN is a float according to IEEE754. All numbers in Javascript being floats, it makes sense that typeof NaN === "number". Now, the fact that IEEE754 is insane is something else, but JS does nothing wrong (aside from not having not-floats)
- Ginden 5y ago> Now, the fact that IEEE754 is insane is something else, but JS does nothing wrong (aside from not having not-floats) JS has BigInt - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- ohgodplsno 5y agoBigInt is relatively recent, all things considered. JS only having floats is a conception flaw.
- dragonwriter 5y agoI mean, it depends on context what “numbers” are acceptable. For an overly general solution: const allowNumbers = { all: () => true, notNaN: n => !Number.isNaN(n), finite: Number.isFinite, integer: Number.isInteger, smol: n => Math.abs(n) < Number.EPSILON, lorge: n => n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER, } function isNumber(n, allow = allowNumbers.all) { return ("number" === typeof n) && allow(n); }
- twic 5y agoThis isn't bad, but for maintainability, it should be split into at least nine packages.
- marshray 5y agoIn the type of things that Javascript considers 'numbers', it's a value used to indicate that it's not of the type of things that math considers 'numbers'.
- JohnHaugeland 5y ago... typeof foo === 'number'
- frankincense 5y agotypeof x === 'number' && !Number.isNaN(x)
- newsbinator 5y agoThis seems like the best solution. Any weird edge cases against this one?
- mirekrusin 5y ago+/- Infinity, better to use Number.isFinite.
- stagas 5y agoInfinities.
- JasonCannon 5y agoComparing it against the list of examples given in is-number, this solution does not properly handle numbers represented as strings, and it also recognizes Infinity as a number.
- TheCoelacanth 5y agoBecause strings aren't numbers and Infinity is a number
- JasonCannon 5y agoStrings are the safest way to hold big numbers in JavaScript due to how JavaScript represents numbers. Also no, infinity is not a number. It's a concept.
- boomlinde 5y agoNumbers is a concept. For some purposes it is useful to consider a number that is infinite, which is why concepts like the extended real number line have been invented.
- the_gipsy 5y agotypeof v === "number" The rest is parsing territory.
- Vilkku 5y agoLooks like the author of these packages agrees. While the is-even and is-odd packages are under the i-voted-for-trump user with the description "This is a joke", the is-number package [1] is still kept under the main profile of the author, the repository is still active on GitHub and there are active issues. [1] https://github.com/jonschlinkert/is-number https://github.com/jonschlinkert/is-number
- nsikorr 5y agoNumber.isFinite() does very well in most cases
- deleted 5y ago[deleted]