Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to perform integer division and get the remainder in JavaScript?
In JavaScript, performing integer division and getting the remainder requires different operators than regular division. The division operator (/) returns a floating-point result, while the remainder operator (%) gives you what's left after division.
The remainder operator (%) returns the remainder when one operand is divided by another. It always takes the sign of the dividend (the first operand). For the operation x % y, x is the dividend and y is the divisor.
Understanding Division vs Remainder
Regular division in JavaScript returns decimal results, but for integer division, we need to use Math.floor() or other methods to get whole numbers.
Method 1: Using the % Operator with Math.floor()
The most straightforward approach combines the division operator with Math.floor() for the quotient and the % operator for the remainder.
Syntax
var quotient = Math.floor(x / y); var remainder = x % y;
Example
<html>
<body>
<h3>Integer Division and Remainder using % operator</h3>
<p id="result"></p>
<script>
let result = document.getElementById("result");
// Basic positive numbers
var x1 = 111, y1 = 8;
var quotient1 = Math.floor(x1 / y1);
var remainder1 = x1 % y1;
result.innerHTML = "111 ÷ 8 = " + quotient1 + " remainder " + remainder1 + "<br>";
// Negative dividend
var x2 = -127, y2 = 8;
var quotient2 = Math.floor(x2 / y2);
var remainder2 = x2 % y2;
result.innerHTML += "(-127) ÷ 8 = " + quotient2 + " remainder " + remainder2 + "<br>";
// Positive dividend, negative divisor
var x3 = 100, y3 = -12;
var quotient3 = Math.floor(x3 / y3);
var remainder3 = x3 % y3;
result.innerHTML += "100 ÷ (-12) = " + quotient3 + " remainder " + remainder3 + "<br>";
</script>
</body>
</html>
111 ÷ 8 = 13 remainder 7 (-127) ÷ 8 = -16 remainder -7 100 ÷ (-12) = -9 remainder 4
Method 2: Using Mathematical Formula
You can also calculate the remainder using the mathematical relationship: remainder = dividend - (divisor × quotient). This method uses parseInt() to get the integer quotient.
Syntax
var quotient = parseInt(x / y); var remainder = x - (y * quotient);
Example
<html>
<body>
<h3>Integer Division using Mathematical Formula</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
var x1 = 17, y1 = 5;
var quotient1 = parseInt(x1 / y1);
var remainder1 = x1 - (y1 * quotient1);
output.innerHTML = "17 ÷ 5 = " + quotient1 + " remainder " + remainder1 + "<br>";
var x2 = 25, y2 = 7;
var quotient2 = parseInt(x2 / y2);
var remainder2 = x2 - (y2 * quotient2);
output.innerHTML += "25 ÷ 7 = " + quotient2 + " remainder " + remainder2 + "<br>";
</script>
</body>
</html>
17 ÷ 5 = 3 remainder 2 25 ÷ 7 = 3 remainder 4
Special Cases
Be aware of these special cases when working with division and remainder:
- Division by zero returns
Infinityand remainder returnsNaN - The remainder operator preserves the sign of the dividend
- For floating-point numbers, use
Math.floor()for proper integer division
Comparison of Methods
| Method | Quotient | Remainder | Best For |
|---|---|---|---|
| % Operator | Math.floor(x / y) | x % y | Most common use cases |
| Mathematical Formula | parseInt(x / y) | x - (y × quotient) | Understanding the concept |
Conclusion
The % operator combined with Math.floor() is the standard way to perform integer division and get remainders in JavaScript. Use the mathematical formula approach when you need to understand the underlying calculation or implement custom division logic.
