AppDividend
Latest Code Tutorials

Javascript Number isInteger() Example | Number.isInteger() Function

0

Javascript Number isInteger() Example | Number.isInteger() Function is today’s topic. The Number.isInteger() method resolves whether the passed value is an integer. The isInteger() method returns true if the value is of the type Number, and an integer (a number without decimals). Otherwise, it returns false. The isInteger() method is not supported in Internet Explorer 11 and earlier versions.

Javascript Number isInteger()

The syntax of the function is following.

Number.isInteger(value)

The value parameter is required, and it is a value that needs to be tested.

If the target value is the integer, return true, otherwise, it returns false.

If a value is NaN or Infinity, it returns false. The method will also return true for floating-point numbers that can be represented as an integer.

See the following code.

// app.js

console.log(Number.isInteger(1));
console.log(Number.isInteger(99999999999999999999999));

See the output.

➜  es git:(master) ✗ node app
true
true
➜  es git:(master) ✗

#Passing a negative number as an argument

If a negative integer value is given to the function as an argument, then the function returns true if the negative value passed to it and it is not of integer type, then a function returns false.

// app.js

console.log(Number.isInteger(-1));
console.log(Number.isInteger(-11.11))
console.log(Number.isInteger(-99999999999999999999999));

See the output.

➜  es git:(master) ✗ node app
true
false
true
➜  es git:(master) ✗

#Passing zero as an argument

If zero is given to the Number.isInteger() function argument then it will return true as zero is also an integer.

console.log(Number.isInteger(0));

See the output.

➜  es git:(master) ✗ node app
true
➜  es git:(master) ✗

#Pass NaN, Infinity, Boolean as an argument

Okay, now let’s pass the NaN, Infinity, Boolean value as an argument and see the output.

// app.js

console.log(Number.isInteger(NaN));
console.log(Number.isInteger(Infinity));
console.log(Number.isInteger(true));

See the output.

➜  es git:(master) ✗ node app
false
false
false
➜  es git:(master) ✗

#Pass Floating point numbers as an argument

See the following code example.

// app.js

const checkInteger = (x, y) => {
  if (Number.isInteger(y / x))
    return 'Pass the condition'
  else
    return 'Does NOT fit for integer'
}
console.log(checkInteger(5, 10))
console.log(checkInteger(5, 11))

See the output.

➜  es git:(master) ✗ node app
Passed condition
Does NOT fit for integer
➜  es git:(master) ✗

Finally, Javascript Number isInteger() Example | Number.isInteger() Function is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.