0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Reverse a Number with Output

You can reverse a number in JavaScript by converting it to a string, splitting it, reversing the array, joining it back, and converting to a number using Number(String(num).split('').reverse().join('')).
📋

Examples

Input12345
Output54321
Input1000
Output1
Input0
Output0
🧠

How to Think About It

To reverse a number, think of it like reversing a word: first change the number into text so you can handle each digit separately, then flip the order of these digits, and finally turn it back into a number to get the reversed value.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string to access each digit.
3
Split the string into an array of characters.
4
Reverse the array to flip the digits.
5
Join the reversed array back into a string.
6
Convert the string back to a number and return it.
💻

Code

javascript
function reverseNumber(num) {
  return Number(String(num).split('').reverse().join(''));
}

console.log(reverseNumber(12345));
Output
54321
🔍

Dry Run

Let's trace reversing the number 12345 through the code

1

Convert number to string

num = 12345 -> String(num) = '12345'

2

Split string into array

'12345' -> ['1', '2', '3', '4', '5']

3

Reverse the array

['1', '2', '3', '4', '5'] -> ['5', '4', '3', '2', '1']

4

Join array back to string

['5', '4', '3', '2', '1'] -> '54321'

5

Convert string to number

Number('54321') -> 54321

StepValue
String(num)'12345'
Split['1','2','3','4','5']
Reverse['5','4','3','2','1']
Join'54321'
Number54321
💡

Why This Works

Step 1: Convert number to string

Using String(num) lets us treat the number like text so we can work with each digit.

Step 2: Reverse the digits

Splitting into an array and reversing it flips the order of digits easily.

Step 3: Convert back to number

Joining the reversed array and converting back to a number gives the reversed numeric value.

🔄

Alternative Approaches

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

console.log(reverseNumber(12345));
This method uses math operations without converting to string, which can be faster for large numbers but is a bit more complex.
Using recursion
javascript
function reverseNumber(num, rev = 0) {
  if (num === 0) return rev;
  return reverseNumber(Math.floor(num / 10), rev * 10 + num % 10);
}

console.log(reverseNumber(12345));
This recursive approach is elegant but can cause stack overflow for very large numbers.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of digits n, as splitting, reversing, and joining each process all take O(n) time.

Space Complexity

Extra space is needed for the array of digits, so space complexity is O(n).

Which Approach is Fastest?

The math-based loop method uses O(1) space and can be faster for very large numbers, while string methods are simpler and more readable.

ApproachTimeSpaceBest For
String methodsO(n)O(n)Simplicity and readability
Math and loopsO(n)O(1)Performance with large numbers
RecursionO(n)O(n)Elegant code but limited by call stack
💡
Convert the number to a string to easily reverse its digits using array methods.
⚠️
Forgetting to convert the reversed string back to a number, which leaves the result as text.