How to check if a variable is an integer in JavaSc

2019-01-01 02:47发布

How do I check if a variable is an integer in JavaScript, and throw an alert if it isn't? I tried this, but it doesn't work:

<html>
    <head>
        <script type="text/javascript">
            var data = 22;
            alert(NaN(data));
        </script>
    </head>
</html>

标签: javascript
30条回答
无与为乐者.
2楼-- · 2019-01-01 03:39

After few successes and failures, I came up with this solution:

const isInt = (value) => {
  return String(parseInt(value, 10)) === String(value)
}

I liked the idea above of checking the value for not being NaN and use parseFloat, but when I tried it in React infrastructure it didn't work for some reason.

Edit: I found a nicer way without using strings:

var isInt = function (str) {
  return str === '0' || !!~~str;
}

I think it's the shortest answer. Maybe even the most efficient, but I could be stand corrected. :)

查看更多
旧时光的记忆
3楼-- · 2019-01-01 03:40

You could tryNumber.isInteger(Number(value)) if value might be an integer in string form e.g var value = "23" and you want this to evaluate to true. Avoid trying Number.isInteger(parseInt(value)) because this won't always return the correct value. e.g if var value = "23abc" and you use the parseInt implementation, it would still return true.

But if you want strictly integer values then probably Number.isInteger(value) should do the trick.

查看更多
不再属于我。
4楼-- · 2019-01-01 03:40

Number.isInteger() is the best way if your browser support it, if not, I think there are so many ways to go:

function isInt1(value){
  return (value^0) === value
}

or:

function isInt2(value){
  return (typeof value === 'number') && (value % 1 === 0); 
}

or:

function isInt3(value){
  return parseInt(value, 10) === value; 
}

or:

function isInt4(value){
  return Math.round(value) === value; 
}

now we can test the results:

var value = 1
isInt1(value)   // return true
isInt2(value)   // return true
isInt3(value)   // return true
isInt4(value)   // return true

var value = 1.1
isInt1(value)   // return false
isInt2(value)   // return false
isInt3(value)   // return false
isInt4(value)   // return false

var value = 1000000000000000000
isInt1(value)   // return false
isInt2(value)   // return true
isInt3(value)   // return false
isInt4(value)   // return true

var value = undefined
isInt1(value)   // return false
isInt2(value)   // return false
isInt3(value)   // return false
isInt4(value)   // return false

var value = '1' //number as string
isInt1(value)   // return false
isInt2(value)   // return false
isInt3(value)   // return false
isInt4(value)   // return false

So, all of these methods are works, but when the number is very big, parseInt and ^ operator would not works well.

查看更多
十年一品温如言
5楼-- · 2019-01-01 03:45

The simplest and cleanest pre-ECMAScript-6 solution (which is also sufficiently robust to return false even if a non-numeric value such as a string or null is passed to the function) would be the following:

function isInteger(x) { return (x^0) === x; } 

The following solution would also work, although not as elegant as the one above:

function isInteger(x) { return Math.round(x) === x; }

Note that Math.ceil() or Math.floor() could be used equally well (instead of Math.round()) in the above implementation.

Or alternatively:

function isInteger(x) { return (typeof x === 'number') && (x % 1 === 0); }

One fairly common incorrect solution is the following:

function isInteger(x) { return parseInt(x, 10) === x; }

While this parseInt-based approach will work well for many values of x, once x becomes quite large, it will fail to work properly. The problem is that parseInt() coerces its first parameter to a string before parsing digits. Therefore, once the number becomes sufficiently large, its string representation will be presented in exponential form (e.g., 1e+21). Accordingly, parseInt() will then try to parse 1e+21, but will stop parsing when it reaches the e character and will therefore return a value of 1. Observe:

> String(1000000000000000000000)
'1e+21'

> parseInt(1000000000000000000000, 10)
1

> parseInt(1000000000000000000000, 10) === 1000000000000000000000
false
查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 03:45

You could use this function:

function isInteger(value) {
    return (value == parseInt(value));
}

It will return true even if the value is a string containing an integer value.
So, the results will be:

alert(isInteger(1)); // true
alert(isInteger(1.2)); // false
alert(isInteger("1")); // true
alert(isInteger("1.2")); // false
alert(isInteger("abc")); // false
查看更多
柔情千种
7楼-- · 2019-01-01 03:46

For positive integer values without separators:

return ( data !== '' && data === data.replace(/\D/, '') );

Tests 1. if not empty and 2. if value is equal to the result of a replace of a non-digit char in its value.

查看更多
登录 后发表回答