Check if a Number is a Positive Integer in JavaScript-Code Examples

Radu TM

Check if a Number is a Positive Integer in JavaScript

JavaScript by Radu TM June 21, 2022

// Check if a number is positive and an integer

const isPositiveInteger = num => {
  // Check if num is a positive number
  if (num > 0) {
    // Use the Number.isInteger() method to check if num is an integer
    return Number.isInteger(num);
  }
  return false;
};

// Test the isPositiveInteger() function
console.log(isPositiveInteger(1)); // true
console.log(isPositiveInteger(-1)); // false
console.log(isPositiveInteger(1.5)); // false

0

21.442

Check if a Number is a Positive Integer in JavaScript

JavaScript by Radu TM June 21, 2022

const isPositiveInteger = (num) => {
  // Check if the Number is greater than or equal to 0
  if (num >= 0) {
    // Check if the Number is an integer (not a decimal)
    if (Number.isInteger(num)) {
      // The Number is a Positive Integer
      return true;
    }
  }
  // The Number is not a Positive Integer
  return false;
};

0

21.442

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories