Home > Web Front-end > JS Tutorial > The simplest implementation method to determine the type of integer in JavaScript

The simplest implementation method to determine the type of integer in JavaScript

PHPz
Release: 2018-09-30 13:38:17
Original
1510 people have browsed it

This article mainly introduces the simplest implementation method for judging integer types in JavaScript. This chapter gives multiple methods for judging integers, and finally summarizes the shortest and most concise implementation method. Friends in need can refer to it.

We know that JavaScript provides the typeof operator, so the easiest thing to think of is to use typeof to determine whether it is a number type.

function isNumber(obj) {
    return typeof obj === 'number'
}
Copy after login

This function has no problem with integers and floating point numbers, but it also returns true for NaN values, which makes people feel uncomfortable. After all, no one will use NaN to do arithmetic operations after using isNumber to judge.

Then improve it and try using Object.prototype.toString.

function isNumber(obj) {
    return Object.prototype.toString.call(obj) === '[object Number]'
}
Copy after login

Same as the typeof judgment, it also returns true for NaN. The amount of code is still large, which is not the desired result. The toString.call method is feasible for judging arrays (Array), but numbers are beyond its capabilities.

As a further improvement, use the isNaN function to deal with NaN values.

function isNumber(obj) {
    return typeof obj === 'number' && !isNaN(obj)
}
Copy after login

This time, if the passed-in is a non-number (NaN or a value that can be converted to NaN), false will be returned

function isNumber(obj) {
    return typeof obj === 'number' && !isNaN(obj)
}
isNumber(1)   // true
isNumber(1.2) // true
isNumber(NaN) // false
isNumber( parseInt('a') ) // false
Copy after login

Well, this isNumber is good, but it’s still There is an equivalent method, using isFinite to judge

function isNumber(obj) {
    return typeof obj === 'number' && isFinite(obj)    
}
Copy after login

. Up to now, the numerical judgment of the shortest code is the third one mentioned in this article that uses the isNaN function. The following is a grand launch of the world's shortest number judgment code

function isNumber(obj) {
    return obj === +obj
}
Copy after login

Returns true for integers and floating point numbers, and returns false for NaN or values ​​that can be converted to NaN.

You don’t understand? Gu~~(╯﹏╰)

Garden friends said that this is not the shortest judgment numeric code in the world, and the parameter obj can be changed to one character. (⊙o⊙) You are right.

By analogy, there is a similar shortest judgment using JS dynamic language features (automatic internal type conversion during operator operation).

// 判断字符串
function isString(obj) {
    return obj === obj+''
}
// 判断布尔类型
function isBoolean(obj) {
    return obj === !!obj
}
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template