Skip to content
Checks if JavaScript variable is strictly a number
JavaScript TypeScript
Branch: master
Clone or download
Drag13 Merge pull request #37 from Drag13/dependabot/npm_and_yarn/mixin-deep…
…-1.3.2

build(deps): Bump mixin-deep from 1.3.1 to 1.3.2
Latest commit 5b79b54 Nov 5, 2019

README.md

IsNumberStrict

Build Status codecov TypeSCript GitHub license

Designed to strictly check if the value can be treated as a number. Works with Number objects, hex an so on. Returns false for strings and other not numbers like {}, undefined, NaN

Install

npm i is-number-strict

Usage

JavaScript with require syntax

const isNumber = require('is-number-strict').default;

console.assert(isNumber(5));
console.assert(!isNumber('5'));

JavaScript with import syntax

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.

typeof new Number(42);
> 'object'

But it is working as good old number

new Number(5) * new Number(6);
> 30

So:

isNumberStrict(new Number(5));
> true

But:

isNumberStrict('5');
> false

And

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.

You can’t perform that action at this time.