is-number
Returns true if the value is a finite number.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
$ npm install --save is-numberWhy is this needed?
In JavaScript, it's not always as straightforward as it should be to reliably check if a value is a number. It's common for devs to use +, -, or Number() to cast a string value to a number (for example, when values are returned from user input, regex matches, parsers, etc). But there are many non-intuitive edge cases that yield unexpected results:
console.log(+[]); //=> 0
console.log(+''); //=> 0
console.log(+' '); //=> 0
console.log(typeof NaN); //=> 'number'This library offers a performant way to smooth out edge cases like these.
Usage
const isNumber = require('is-number');See the tests for more examples.
true
False
Everything else is false, as you would expect:
Release history
7.0.0
Refactor. Now uses
.isFiniteif it exists.Performance is about the same as v6.0 when the value is a string or number. But it's now 3x-4x faster when the value is not a string or number.
6.0.0
Optimizations, thanks to @benaadams.
5.0.0
Breaking changes
removed support for
instanceof Numberandinstanceof String
Benchmarks
As with all benchmarks, take these with a grain of salt. See the benchmarks for more detail.
About
Related projects
You might also be interested in these projects:
is-plain-object: Returns true if an object was created by the
Objectconstructor. | homepageis-primitive: Returns
trueif the value is a primitive. | homepage
Contributors
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on June 15, 2018.
Last updated