• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Check If Number is a Whole Number

JavaScript Check If Number is a Whole Number

May 8, 2022 Leave a Comment

In JavaScript, the easiest way to check if a number is a whole number is by using the Number.isInteger() method.

console.log(Number.isInteger(2.0))
console.log(Number.isInteger(2.1))

#Output:
True
False

In JavaScript, when working with numbers, it can be useful to be able to find out if a number is a whole number.

We can easily check if a number is a whole number with the help of the Number.isInteger() method.

The isInteger() method returns a boolean indicating if the number is an integer or not.

JavaScript Check If Number is a Whole Number With a Click

Below we will provide code to let the user input a number, and then use the isInteger() method to check if the number is a whole number. Here is our simple HTML setup:

<p>Type a number to see if it is a whole number:</p>
<input id="userVal" type="text" value="" onkeyup="checkNum(event)">
<input id="submitNum" type="submit" value="Submit" onclick="checkNum('click')">
<div id="results"></div>

Below is the JavaScript code which take the user input using the onkeyup event or the onclick event along with the value property and use the Number.isInteger() method on that user input and update the results below using the textContent property.

Here is the JavaScript code:

function checkNum(e) {
  //If we clicked or pressed enter, run the following code 
  if( e == 'click' || e.keyCode == 13 ){
    var userNum = document.getElementById("userVal").value;

    //Check if number is whole
     var isWhole = Number.isInteger(Number(userNum));

    //Show the result to the user to the user
    document.getElementById("results").textContent = isWhole;   
  }
};

The final code and output for this example are below:

Code Output:

Type a number to see if it is a whole number:


Full Code:

<p>Type a number to see if it is a whole number:</p>
<input id="userVal" type="text" value="" onkeyup="checkNum(event)">
<input id="submitNum" type="submit" value="Submit" onclick="checkNum('click')">
<div id="results"></div>

<script>

function checkNum(e) {
  if( e == 'click' || e.keyCode == 13 ){
    var userNum = document.getElementById("userVal").value;
    var isWhole = Number.isInteger(Number(userNum));
    document.getElementById("results").textContent = isWhole;   
  }
};

</script>

Hopefully this article has been useful in helping you understand how to use JavaScript to check if whole number.

Other Articles You'll Also Like:

  • 1.  How to Count Vowels in a String Using JavaScript
  • 2.  How to Use JavaScript to Check if a String is Empty
  • 3.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 4.  Using JavaScript to Round to 1 Decimal
  • 5.  JavaScript Change Border Color of Element
  • 6.  Using JavaScript to Round to 2 Decimal Places
  • 7.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 8.  How to Remove Decimals of a Number in JavaScript
  • 9.  JavaScript Check if Attribute Exists in HTML Element
  • 10.  Check That String Does Not Contain Character in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy