Check if String is a Positive Integer in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if String is a Positive Integer #

To check if a string is a positive integer:

  1. Convert the string to a number and pass it to the Number.isInteger() method. The method returns true if the provided value is an integer and false otherwise.
  2. Check if the number is greater than 0.
  3. If both conditions are met, then the string is a valid positive integer.
index.js
function isPositiveInteger(str) { if (typeof str !== 'string') { return false; } const num = Number(str); if (Number.isInteger(num) && num > 0) { return true; } return false; } console.log(isPositiveInteger('0')); // 👉️ false console.log(isPositiveInteger('1')); // true console.log(isPositiveInteger('-1')); // 👉️ false console.log(isPositiveInteger('1.5')); // 👉️ false console.log(isPositiveInteger('test')); // false console.log(isPositiveInteger('')); // 👉️ false console.log(isPositiveInteger(' ')); // 👉️ false

We first check if the passed in value isn't a string. In this case, we return false straight away because the function only handles strings.

We then convert the string to a number and store the value into a variable.

Here are some examples of converting a string to a number.

index.js
console.log(Number('1')); // 👉️ 1 console.log(Number('1.5')); // 👉️ 1.5 console.log(Number('')); // 👉️ 0 console.log(Number(' ')); // 👉️ 0 console.log(Number('hello')); // 👉️ NaN
If the string contains any non-numeric characters (not 0-9), we get NaN (not a number) back.

If it's an empty string or contains spaces, we get 0 back.

The first condition in our second if statement checks if the value is a valid integer.

index.js
function isPositiveInteger(str) { if (typeof str !== 'string') { return false; } const num = Number(str); if (Number.isInteger(num) && num > 0) { return true; } return false; }

The Number.isInteger method returns true if the provided value is an integer and false otherwise.

Here are some examples of using the Number.isInteger method directly.

index.js
console.log(Number.isInteger(1)); // 👉️ true console.log(Number.isInteger(1.5)); // 👉️ false console.log(Number.isInteger('1')); // 👉️ false console.log(Number.isInteger(-1)); // 👉️ true

We used the logical AND (&&) operator to chain another condition. The if block is only run if both conditions are true.

The second condition checks if the number is greater than 0.

If the number is an integer and is greater than 0, we have a positive integer.

However, there is a caveat with the Number.isInteger method. The method returns true if the passed in value:

  • is an integer
  • is a float that can be represented as an integer

Here are examples of floats that can be represented as integers:

index.js
// 👇️ true console.log(Number.isInteger(10.0)); // 👇️ true console.log(Number.isInteger(10.000000000000000123));

This shouldn't matter as most applications are OK with considering numbers such as 1.0 and 2.0 to be integers.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.