Open In App

JavaScript Program to Reverse Digits of a Number

Last Updated : 18 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about Reverse Number Programs in JavaScript. A reverse number in JavaScript involves reversing the digits of a given number. It’s done by manipulating the digit’s order, resulting in the number’s reversed form.

reverse

There are several methods that can be used to Reverse the Numbers by using JavaScript, which is listed below:

Using String Reversal

In this approach, we are using string reversal, converting a number to a string, reverse it using split(”).reverse().join(”), and convert back to a number.

Syntax:

let result = num1.toString().split('').reverse().join('');

Example: In this example, we are using the above-explained approach.

Javascript




let num1 = 123456789;
let result = num1.toString().split('').reverse().join('');
console.log(result);


Output

987654321

Using Array Reduce() Method

In this approach, Using the reduce() method, reverse the digits of a number. Convert the number to an array of its digits, then employ the reduce() function with a spread operator to reverse the order.

Syntax:

function reverseFunction(num) {
let digits = Array.from(String(num), Number);
let reversedArray = digits.reduce((acc, digit) =>
[digit, ...acc], []);
return parseInt(reversedArray.join(''));
};

Example: In this example we are using above explained approach.

Javascript




function reverseFunction(num) {
    let digits = Array.from(String(num), Number);
    let reversedArray = digits.reduce((acc, digit) =>
        [digit, ...acc], []);
    return parseInt(reversedArray.join(''));
}
 
let num = 123456789;
let reversedNum = reverseFunction(num);
 
console.log(reversedNum);


Output

987654321

Using String Iteration

In this approach, using string iteration,we convert number to string, iterate backward using a loop, and construct reversed string by appending digits. Convert back to a number.

Syntax:

function reverseFunction(num) {
let numStr = num.toString();
let reversedStr = '';
for (let i = numStr.length - 1; i >= 0; i--) {
reversedStr += numStr[i];
}
return parseInt(reversedStr);
};

Example: In this example we are using the above-explained approach.

Javascript




function reverseFunction(num) {
    let numStr = num.toString();
    let reversedStr = '';
    for (let i = numStr.length - 1; i >= 0; i--) {
        reversedStr += numStr[i];
    }
    return parseInt(reversedStr);
}
 
let num = 987654321;
let reversedNum = reverseFunction(num);
 
console.log(reversedNum);


Output

123456789

Using Recursion

In this approach,using recursion, reverse digits of a number. Recursively divide the number by 10, build reversed number by accumulating digits, and return the result.

Syntax:

function reverseFunction(num, reversed = 0) {
if (num === 0) {
return reversed;
}
return reverseFunction(Math.floor(num / 10),
reversed * 10 + num % 10);
};

Example: In this example, the reverseFunction takes a number and a reversed parameter as inputs. It uses recursion to reverse the digits by repeatedly dividing the number by 10 to isolate the last digit, while building the reversed number by multiplying it by 10 and adding the current last digit.

Javascript




function reverseFunction(num, reversed = 0) {
    if (num === 0) {
        return reversed;
    }
    return reverseFunction(Math.floor(num / 10),
        reversed * 10 + num % 10);
}
 
const num = 987654321;
const result = reverseFunction(num);
 
console.log(result);


Output

123456789


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads