What Is Integer Data Type With Example?
Let’s take a simple example to understand how integer data type works in HTML. Suppose we have a form where users can enter their age:
In the above example, the input field with the number type restricts the user to enter only integer values. If the user tries to enter a decimal number, an error message will be displayed.
Integers can be used in various mathematical operations such as addition, subtraction, multiplication, and division. Let’s see some examples:
To add two integers together, use the plus (+) operator:
<script> var num1 = 10; var num2 = 5; var sum = num1 + num2; console.log(sum); // Output: 15 </script>
To subtract one integer from another, use the minus (-) operator:
<script> var num1 = 10; var num2 = 5; var difference = num1 - num2; console.log(difference); // Output: 5 </script>
To multiply two integers together, use the asterisk (*) operator:
<script> var num1 = 10; var num2 = 5; var product = num1 * num2; console.log(product); // Output: 50 </script>
To divide one integer by another, use the forward slash (/) operator:
<script> var num1 = 10; var num2 = 5; var quotient = num1 / num2; console.log(quotient); // Output: 2 </script>