Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Reverse a Number in JavaScript

Reverse a number in JavaScript using % 10 and Math.floor, string methods and negative support. Learn why 100 becomes 1, with explanation and output.

BeginnerJavaScript Loop ProgramsExample 4 of 6
reverse-a-number.js
1// Method 1: Using while loop
2function reverseNumber(num) {
3 let reversed = 0;
4 let original = num;
5
6 while (num > 0) {
7 let digit = num % 10;
8 reversed = reversed * 10 + digit;
9 num = Math.floor(num / 10);
10 }
11
12 return reversed;
13}
14
15console.log("Reverse of 1234:", reverseNumber(1234));
16console.log("Reverse of 5678:", reverseNumber(5678));
17
18// Method 2: Using string conversion
19function reverseNumberString(num) {
20 return parseInt(num.toString().split('').reverse().join(''));
21}
22
23console.log("\nUsing string method:");
24console.log("Reverse of 1234:", reverseNumberString(1234));
25console.log("Reverse of 9876:", reverseNumberString(9876));
26
27// Method 3: Using array methods
28function reverseNumberArray(num) {
29 return parseInt(
30 num.toString()
31 .split('')
32 .reverse()
33 .join('')
34 );
35}
36
37console.log("\nUsing array methods:");
38console.log("Reverse of 5432:", reverseNumberArray(5432));
39
40// Method 4: Handling negative numbers
41function reverseNumberComplete(num) {
42 let isNegative = num < 0;
43 num = Math.abs(num);
44
45 let reversed = 0;
46 while (num > 0) {
47 reversed = reversed * 10 + (num % 10);
48 num = Math.floor(num / 10);
49 }
50
51 return isNegative ? -reversed : reversed;
52}
53
54console.log("\nWith negative support:");
55console.log("Reverse of -1234:", reverseNumberComplete(-1234));
56console.log("Reverse of 5678:", reverseNumberComplete(5678));

Sample Output

Reverse of 1234: 4321
Reverse of 5678: 8765

Using string method:
Reverse of 1234: 4321
Reverse of 9876: 6789

Using array methods:
Reverse of 5432: 2345

With negative support:
Reverse of -1234: -4321
Reverse of 5678: 8765

In this one

  • How % 10 takes the last digit
  • How reversed * 10 + digit builds the result
  • How to reverse with string methods
  • How to handle negative numbers and lost zeros

How it works

  1. 1

    reverseNumber(1234) peels digits 4, 3, 2, 1.

  2. 2

    Each digit is added to reversed * 10.

  3. 3

    The string method reverses "1234" into "4321".

  4. 4

    reverseNumberComplete(-1234) reverses 1234 and adds the minus sign back.

reverseNumber(1234)

Values after each loop round.

  1. 1.digit 4 → reversed 4, num 123
  2. 2.digit 3 → reversed 43, num 12
  3. 3.digit 2 → reversed 432, num 1
  4. 4.digit 1 → reversed 4321, num 0 (stop)

Edge cases

Trailing zeros

reverseNumber(100) returns 1.

Numbers cannot start with zeros. Keep the result as a string if you need "001".

Negative numbers in Method 1

reverseNumber(-12) returns 0.

The loop condition num > 0 is false at once. Use Method 4.

Decimals

reverseNumber(12.5) gives a strange result.

This logic is for whole numbers. Reject decimals first.

What's going on

This program turns 1234 into 4321. Reversing digits is a common test question and is the first step of a number palindrome check.

Method 1 uses math only. In each round, num % 10 gives the last digit. For 1234 that is 4. Then reversed = reversed * 10 + digit moves the old digits one place left and adds the new digit at the end. Math.floor(num / 10) removes the last digit from num. The loop runs while num is greater than 0.

Trace it for 1234. reversed goes 4, 43, 432, 4321. num goes 123, 12, 1, 0, and the loop stops.

Method 2 converts the number to a string, splits it into characters, reverses the array, joins it back, and turns it into a number with parseInt. Method 3 is the same chain written over several lines.

Method 4 handles negative numbers. The while loop only runs for positive values, so -1234 would return 0. The fix is to remember the sign, work on Math.abs(num), and put the minus back at the end.

Trailing zeros disappear. 100 reversed is 1, not 001, because numbers do not keep leading zeros. If you really need "001", keep the answer as a string: String(n).split('').reverse().join('').

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is 5678 % 10?

Pick an answer to check it.

Question 2

What does reversing 120 give as a number?

Pick an answer to check it.

Questions people ask

Why use Math.floor(num / 10)?

Division gives a decimal like 123.4. Math.floor drops the decimal part so the last digit is removed.

Which method is faster?

The math loop avoids creating strings and arrays, but both are fast for normal numbers.

Why is the variable original never used?

It is left over from a palindrome version. You can delete it here.