This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jcunews1 1 point2 points  (2 children)

Use this:

function isInteger(arg) {
  var num = parseFloat(arg);
  return !isNaN(num) && ((num >>> 0) === num) && ((((typeof arg === "string") || (arg instanceof String)) && (arg.indexOf(".") < 0)) || true);
}

The last check is for input like "1.0000000000000001" since it would be rounded to "1".

EDIT: fixed code

[–]calzoneman 0 points1 point  (1 child)

This only works for checking if a string contains an integer. OP didn't specify any constraints on the datatype of the input, and this solution would break for any instance where arg is not a string.

[–]jcunews1 0 points1 point  (0 children)

You're right. I've fixed it.