js several ways to judge whether it is a number

一、typeof、instanceof、Number.isInteger

  • typeof
    typeofjudges whether the value is a basic type number:
const num = 1;
typeof num === 'number'; // true
  • instanceof
    instanceofjudges whether the value is a wrapper class Number:
const num = new Number(1);
num instanceof Number; // true
  • Number.isInteger
    Number.isIntegerdetermines whether the value is 整数:
Number.isInteger(1);   // true
Number.isInteger('1'); // false
Number.isInteger(1.1); // false

The disadvantages of these methods are that they can only be judged based on the type, and it is impossible to judge whether the string is a value.

二、parseInt、parseFloat

parseIntparseFloatWhen an illegal character is encountered during parsing and parsing, the parsed value will be returned 数值. That is to say, as long as the string 头部is 合法数值, then the value can be parsed, even if the whole is not a value. For example 123abc, it will be parsed 123.

const a = '123abc';
parseFloat(a); // 123
const b = 'a123abc';
parseFloat(b); // NaN
const c = '0123abc';
parseFloat(c); // 123
const d = 'a0123abc';
parseFloat(d); // NaN

Three, isNaN, isFinite

  • NaN, it says Not-a-Number. Two NaNs cannot be compared directly for equality, because we only know that they are not numbers, and what they are is uncertain, so they cannot be compared equal.
  • isNaN(value), if Number(value)the result is NaNreturned true, otherwise return false.
  • isFinite(value), if Number(value)the result of 数值and is not equal to Infinityor -Infinityreturn true, otherwise return false.
Number(true); // 1
Number(false); // 0
Number(null); // 0
Number(undefined); // NaN
Number(''); // 0
Number('123'); // 123
Number(123); // 123
Number('abc'); // NaN

isNaN(true); // false
isNaN(false); // false
isNaN(null); // false
isNaN(undefined); // true
isNaN(''); // false
isNaN('123'); // false
isNaN(123); // false
isNaN('abc'); // true

isFinite(true); // true
isFinite(false); // true
isFinite(null); // true
isFinite(undefined); // false
isFinite(''); // true
isFinite('123'); // true
isFinite(123); // true
isFinite('abc'); // false

四、Number.isNaN、Number.isFinite

  • Number.isNaN(value), if valueit is NaNreturned true, otherwise it is returned false.
  • Number.isFinite(value), if valuenumeric and not equal Infinityor -Infinityreturn true, otherwise return false.
  • Compared with the global function isNaN(), Number.isNaN()it will not convert the parameter to a number by itself, and NaNwill only return if the parameter is a number with a value of true.
Number.isNaN(NaN);        // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0)       // true
Number.isNaN(true); // false
Number.isNaN(false); // false
Number.isNaN(null); // false
Number.isNaN(undefined); // false
Number.isNaN(''); // false
Number.isNaN('123'); // false
Number.isNaN(123); // false
Number.isNaN('abc'); // false

Number.isFinite(true); // false
Number.isFinite(false); // false
Number.isFinite(null); // false
Number.isFinite(undefined); // false
Number.isFinite(''); // false
Number.isFinite('123'); // false
Number.isFinite(123); // true
Number.isFinite('abc'); // false
  • Number.isNaNEquivalent to:
Number.isNaN = Number.isNaN || function(value) {
    
    
    return typeof value === "number" && isNaN(value);
}
  • Number.isFiniteEquivalent to:
if (Number.isFinite === undefined) Number.isFinite = function(value) {
    
    
    return typeof value === 'number' && isFinite(value);
}

Five, regular expressions

const exp = /^[0-9]+.?[0-9]*/;
exp.test('123');   // true
exp.test('a123'); // false
exp.test(123); // true

reference

Guess you like

Origin blog.csdn.net/letianxf/article/details/129724347