一、typeof、instanceof、Number.isInteger
- typeof
typeof
judges whether the value is a basic typenumber
:
const num = 1;
typeof num === 'number'; // true
- instanceof
instanceof
judges whether the value is a wrapper classNumber
:
const num = new Number(1);
num instanceof Number; // true
- Number.isInteger
Number.isInteger
determines 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
parseInt
parseFloat
When 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 saysNot-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)
, ifNumber(value)
the result isNaN
returnedtrue
, otherwise returnfalse
.isFinite(value)
, ifNumber(value)
the result of数值
and is not equal toInfinity
or-Infinity
returntrue
, otherwise returnfalse
.
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)
, ifvalue
it isNaN
returnedtrue
, otherwise it is returnedfalse
.Number.isFinite(value)
, ifvalue
numeric and not equalInfinity
or-Infinity
returntrue
, otherwise returnfalse
.- Compared with the global function
isNaN()
,Number.isNaN()
it will not convert the parameter to a number by itself, andNaN
will only return if the parameter is a number with a value oftrue
.
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.isNaN
Equivalent to:
Number.isNaN = Number.isNaN || function(value) {
return typeof value === "number" && isNaN(value);
}
Number.isFinite
Equivalent 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