Is 16 A Whole Number
In JavaScript, the easiest way to cheque if a number is a whole number is by using the Number.isInteger() method.
console.log(Number.isInteger(2.0)) panel.log(Number.isInteger(ii.one)) #Output: True False
In JavaScript, when working with numbers, information technology can be useful to be able to detect out if a number is a whole number.
We can easily cheque 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 volition provide code to let the user input a number, and then utilise the isInteger() method to check if the number is a whole number. Here is our unproblematic 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 lawmaking which take the user input using the onkeyup event or the onclick event along with the value property and apply the Number.isInteger() method on that user input and update the results below using the textContent property.
Hither is the JavaScript code:
role checkNum(east) { //If we clicked or pressed enter, run the post-obit code if( eastward == 'click' || e.keyCode == 13 ){ var userNum = certificate.getElementById("userVal").value; //Bank check if number is whole var isWhole = Number.isInteger(Number(userNum)); //Bear witness the result to the user to the user document.getElementById("results").textContent = isWhole; } };
The terminal code and output for this instance are beneath:
Code Output:
Type a number to see if it is a whole number:
Full Code:
<p>Type a number to come across 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 yous sympathize how to use JavaScript to check if whole number.
About The Programming Skillful
The Programming Practiced is a compilation of a programmer'due south findings in the earth of software development, website creation, and automation of processes.
Programming allows u.s. to create astonishing applications which make our work more efficient, repeatable and accurate.
At the finish of the day, nosotros desire to exist able to merely push a button and allow the lawmaking do it'south magic.
You tin can read more about united states of america on our about page.
Reader Interactions
Is 16 A Whole Number,
Source: https://theprogrammingexpert.com/javascript-check-if-number-is-a-whole-number/
Posted by: deckeronsts1988.blogspot.com
0 Response to "Is 16 A Whole Number"
Post a Comment