https://github.com/drag13/isnumberstrict
Checks if JavaScript variable is a number. Strings are not allowed.
https://github.com/drag13/isnumberstrict
isnumber javascript library npm-package number numbers typecheck typechecking
Last synced: 7 months ago
JSON representation
Checks if JavaScript variable is a number. Strings are not allowed.
- Host: GitHub
- URL: https://github.com/drag13/isnumberstrict
- Owner: Drag13
- License: mit
- Created: 2018-07-16T14:58:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T08:44:33.000Z (about 3 years ago)
- Last Synced: 2024-03-31T19:10:15.686Z (almost 2 years ago)
- Topics: isnumber, javascript, library, npm-package, number, numbers, typecheck, typechecking
- Language: JavaScript
- Homepage:
- Size: 1.05 MB
- Stars: 11
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[
](https://savelife.in.ua/en/donate/)
# IsNumberStrict - check if something is a number
[](https://img.shields.io/npm/dm/is-number-strict)
[](https://img.shields.io/bundlephobia/minzip/is-number-strict)
[](https://travis-ci.org/Drag13/IsNumberStrict)
[](https://codecov.io/gh/Drag13/isnumberstrict)
[](https://github.com/Drag13/IsNumberStrict)
[](https://github.com/Drag13/IsNumberStrict/blob/master/LICENSE)
Designed to strictly check if the value is a number. Works with Number objects, hex, and so on. Returns `false` for `string` and other not numbers like `{}`, `undefined`, `NaN`
## Install
```cmd
npm i is-number-strict
```
## Usage
JavaScript with require syntax
```javascript
const isNumber = require('is-number-strict').default;
console.assert(isNumber(5));
console.assert(!isNumber('5'));
```
JavaScript with import syntax
```javascript
import isNumber from "is-number-strict";
console.assert(isNumber(5));
console.assert(!isNumber('5'));
```
## What problem it solves
This tiny lib tries to make type assertion little bit more predictable and remove NaN from your calculations.
```javascript
typeof new Number(42);
> 'object'
```
But it is working as good old number
```javascript
new Number(5) * new Number(6);
> 30
```
So:
```javascript
isNumberStrict(new Number(5));
> true
```
But:
```javascript
isNumberStrict('5');
> false
```
And
```javascript
isNumberStrict(NaN);
> false
```
> Why didn't you treat '5' as a number? '5' + 5 = 10!
Yes, but 5 + '5' = '55' and 5 * '5' = NaN. I don't want to see NaN or '55' in my calculations.
So if you want to have more predictable type checking - check my tests and welcome!
## Some wired cases
> Why do you treat new Number([]) as a number?
Because JS will evaluate it to 0, and 0 is number.
> Why do you treat new number({}) as not a number?
Because JS will evaluate it to NaN and NaN is not a number according to my purposes.