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:29

Why hasnt anyone mentioned Number.isInteger() ?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger

Works perfectly for me and solves the issue with the NaN beginning a number.

查看更多
还给你的自由
3楼-- · 2019-01-01 03:31

This will solve one more scenario (121.), a dot at end

function isInt(value) {
        var ind = value.indexOf(".");
        if (ind > -1) { return false; }

        if (isNaN(value)) {
            return false;
        }

        var x = parseFloat(value);
        return (x | 0) === x;

    }
查看更多
浮光初槿花落
4楼-- · 2019-01-01 03:31

My approach:

function isInteger(a){
    return a >= 1e+21 ? true : a === (+a|0);
}

// tests
[
  1, 
  1.0, 
  1.0000000000001,
  0.1, 
  "0",
  "1", 
  "1.1", 
  4e2, 
  1000000000000000000000,
  NaN,
  [],
  {},
  true,
  false,
  null,
  undefined,
  Infinity
].forEach( a => console.log(typeof a, a, isInteger(a)) );

查看更多
无与为乐者.
5楼-- · 2019-01-01 03:32

Use the === operator (strict equality) as below,

if (data === parseInt(data, 10))
    alert("data is integer")
else
    alert("data is not an integer")
查看更多
有味是清欢
6楼-- · 2019-01-01 03:32

You can use a simple regular expression:

function isInt(value) {
    var er = /^-?[0-9]+$/;
    return er.test(value);
}
查看更多
人间绝色
7楼-- · 2019-01-01 03:33

That depends, do you also want to cast strings as potential integers as well?

This will do:

function isInt(value) {
  return !isNaN(value) && 
         parseInt(Number(value)) == value && 
         !isNaN(parseInt(value, 10));
}

With Bitwise operations

Simple parse and check

function isInt(value) {
  var x = parseFloat(value);
  return !isNaN(value) && (x | 0) === x;
}

Short-circuiting, and saving a parse operation:

function isInt(value) {
  if (isNaN(value)) {
    return false;
  }
  var x = parseFloat(value);
  return (x | 0) === x;
}

Or perhaps both in one shot:

function isInt(value) {
  return !isNaN(value) && (function(x) { return (x | 0) === x; })(parseFloat(value))
}

Tests:

isInt(42)        // true
isInt("42")      // true
isInt(4e2)       // true
isInt("4e2")     // true
isInt(" 1 ")     // true
isInt("")        // false
isInt("  ")      // false
isInt(42.1)      // false
isInt("1a")      // false
isInt("4e2a")    // false
isInt(null)      // false
isInt(undefined) // false
isInt(NaN)       // false

Here's the fiddle: http://jsfiddle.net/opfyrqwp/28/

Performance

Testing reveals that the short-circuiting solution has the best performance (ops/sec).

// Short-circuiting, and saving a parse operation
function isInt(value) {
  var x;
  if (isNaN(value)) {
    return false;
  }
  x = parseFloat(value);
  return (x | 0) === x;
}

Here is a benchmark: http://jsben.ch/#/htLVw

If you fancy a shorter, obtuse form of short circuiting:

function isInt(value) {
  var x;
  return isNaN(value) ? !1 : (x = parseFloat(value), (0 | x) === x);
}

Of course, I'd suggest letting the minifier take care of that.

查看更多
登录 后发表回答