0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Reverse Number Using While Loop

You can reverse a number in JavaScript using a while loop by extracting digits with num % 10, building the reversed number with reversed = reversed * 10 + digit, and reducing the original number with Math.floor(num / 10) until it becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input0
Output0
🧠

How to Think About It

To reverse a number, think of peeling off its last digit repeatedly and adding that digit to a new number from right to left. Use the remainder operator % to get the last digit, then add it to the reversed number after shifting the reversed number's digits left by multiplying by 10. Remove the last digit from the original number by dividing by 10 and taking the floor. Repeat until the original number is zero.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number as 0.
3
While the input number is greater than 0:
4
Extract the last digit using remainder operator.
5
Add the digit to reversed number after shifting digits left.
6
Remove the last digit from the input number by dividing by 10 and taking floor.
7
Return the reversed number.
💻

Code

javascript
function reverseNumber(num) {
  let reversed = 0;
  while (num > 0) {
    let digit = num % 10;
    reversed = reversed * 10 + digit;
    num = Math.floor(num / 10);
  }
  return reversed;
}

console.log(reverseNumber(1234));
Output
4321
🔍

Dry Run

Let's trace reversing the number 1234 through the code

1

Start

num = 1234, reversed = 0

2

First loop iteration

digit = 1234 % 10 = 4; reversed = 0 * 10 + 4 = 4; num = Math.floor(1234 / 10) = 123

3

Second loop iteration

digit = 123 % 10 = 3; reversed = 4 * 10 + 3 = 43; num = Math.floor(123 / 10) = 12

4

Third loop iteration

digit = 12 % 10 = 2; reversed = 43 * 10 + 2 = 432; num = Math.floor(12 / 10) = 1

5

Fourth loop iteration

digit = 1 % 10 = 1; reversed = 432 * 10 + 1 = 4321; num = Math.floor(1 / 10) = 0

6

End

num is 0, loop ends, reversed = 4321

numdigitreversed
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the extracted digit to append it.

Step 3: Remove last digit

Divide the original number by 10 and use Math.floor to drop the last digit, preparing for the next loop.

🔄

Alternative Approaches

Convert to string and reverse
javascript
function reverseNumberString(num) {
  return Number(num.toString().split('').reverse().join(''));
}
console.log(reverseNumberString(1234));
This method is simpler but uses string operations and extra memory.
Recursive approach
javascript
function reverseNumberRecursive(num, reversed = 0) {
  if (num === 0) return reversed;
  return reverseNumberRecursive(Math.floor(num / 10), reversed * 10 + num % 10);
}
console.log(reverseNumberRecursive(1234));
Uses recursion instead of a loop, which can be elegant but risks call stack limits for very large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The while loop method is efficient and uses constant space, while string conversion uses extra memory and recursion adds call stack overhead.

ApproachTimeSpaceBest For
While loopO(d)O(1)Efficient and memory-friendly
String conversionO(d)O(d)Simple code, less efficient for large numbers
RecursiveO(d)O(d)Elegant but risks stack overflow
💡
Use Math.floor to safely remove the last digit when dividing by 10.
⚠️
Forgetting to use Math.floor causes the number to become a decimal and breaks the loop.