Reverse a Number

Program to reverse digits of a number

BeginnerTopic: Loop Programs
Back

JavaScript Reverse a Number Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Method 1: Using while loop
function reverseNumber(num) {
    let reversed = 0;
    let original = num;
    
    while (num > 0) {
        let digit = num % 10;
        reversed = reversed * 10 + digit;
        num = Math.floor(num / 10);
    }
    
    return reversed;
}

console.log("Reverse of 1234:", reverseNumber(1234));
console.log("Reverse of 5678:", reverseNumber(5678));

// Method 2: Using string conversion
function reverseNumberString(num) {
    return parseInt(num.toString().split('').reverse().join(''));
}

console.log("\nUsing string method:");
console.log("Reverse of 1234:", reverseNumberString(1234));
console.log("Reverse of 9876:", reverseNumberString(9876));

// Method 3: Using array methods
function reverseNumberArray(num) {
    return parseInt(
        num.toString()
           .split('')
           .reverse()
           .join('')
    );
}

console.log("\nUsing array methods:");
console.log("Reverse of 5432:", reverseNumberArray(5432));

// Method 4: Handling negative numbers
function reverseNumberComplete(num) {
    let isNegative = num < 0;
    num = Math.abs(num);
    
    let reversed = 0;
    while (num > 0) {
        reversed = reversed * 10 + (num % 10);
        num = Math.floor(num / 10);
    }
    
    return isNegative ? -reversed : reversed;
}

console.log("\nWith negative support:");
console.log("Reverse of -1234:", reverseNumberComplete(-1234));
console.log("Reverse of 5678:", reverseNumberComplete(5678));
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

Understanding Reverse a Number

This program demonstrates different methods to reverse a number.

Method 1: Mathematical Approach

Using modulo and division:

while (num > 0) {
    let digit = num % 10;        // Get last digit
    reversed = reversed * 10 + digit;  // Add to reversed
    num = Math.floor(num / 10);   // Remove last digit
}

How it works:

1.Extract last digit: num % 10
2.Add to reversed: reversed * 10 + digit
3.Remove last digit: Math.floor(num / 10)

Example: 1234

Iteration 1: digit=4, reversed=4, num=123
Iteration 2: digit=3, reversed=43, num=12
Iteration 3: digit=2, reversed=432, num=1
Iteration 4: digit=1, reversed=4321, num=0

Method 2: String Conversion

Convert to string, reverse, convert back:

num.toString().split('').reverse().join('')

String Methods:

toString(): Number to string
split(''): String to array
reverse(): Reverse array
join(''): Array to string
parseInt(): String to number

Method 3: Array Methods

Same as Method 2, more explicit:

num.toString()
   .split('')
   .reverse()
   .join('')

Method 4: Negative Number Support

Handle negative numbers:

let isNegative = num < 0;
num = Math.abs(num);  // Make positive
// ... reverse logic ...
return isNegative ? -reversed : reversed;

Math Methods:

Math.abs(): Absolute value
Math.floor(): Round down

When to Use:

-

Mathematical

: Learning, efficient

-

String

: Simple, readable

-

Array

: Functional style

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Reverse a Number

This JavaScript program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents