Merge pull request #937 from mkopa/935-isNumber

[FIX] #935 isNumber is wrong for NaN
This commit is contained in:
Angelos Chalaris
2019-03-19 09:42:31 +02:00
committed by GitHub
2 changed files with 8 additions and 3 deletions

View File

@ -2,13 +2,15 @@
Checks if the given argument is a number.
Use `typeof` to check if a value is classified as a number primitive.
Use `typeof` to check if a value is classified as a number primitive.
To safeguard against `NaN`, check if `val === val` (as `NaN` has a `typeof` equal to `number` and is the only value not equal to itself).
```js
const isNumber = val => typeof val === 'number';
const isNumber = val => typeof val === 'number' && val === val;
```
```js
isNumber('1'); // false
isNumber(1); // true
isNumber('1'); // false
isNumber(NaN); // false
```