JavaScript Program to Reverse Digits of a Number
Last Updated :
21 Aug, 2024
Reversing the digits of a number means rearranging its digits in the opposite order. For instance, reversing 1234 gives 4321. In JavaScript, this is typically achieved by converting the number to a string, reversing the characters, and converting it back to a number, offering efficient manipulation.

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);
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 the reverseFunction converts a number to an array of digits, reverses it, and joins it back into a number.
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);
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 the reverseFunction converts a number to a string, reverses it character by character, and converts it back to a number.
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);
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);
Similar Reads
JavaScript Program for Sum of Digits of a Number
In this article, we are going to learn about finding the Sum of Digits of a number using JavaScript. The Sum of Digits refers to the result obtained by adding up all the individual numerical digits within a given integer. It's a basic arithmetic operation. This process is repeated until a single-dig
3 min read
JavaScript Program to Check for Palindrome Number
We are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output :
4 min read
JavaScript Program to Check Whether a Number is Harshad Number
A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it's a Harshad number. For example, 18 is a Harshad number because the sum of its dig
2 min read
JavaScript Program to Multiply the Given Number by 2 such that it is Divisible by 10
In this article, we are going to implement a program through which we can find the minimum number of operations needed to make a number divisible by 10. We have to multiply it by 2 so that the resulting number will be divisible by 10. Our task is to calculate the minimum number of operations needed
3 min read
JavaScript Program to Convert Decimal to Binary
In this article, we are going to learn the conversion of numeric values from decimal to binary. Binary is a number system with 2 digits (0 and 1) representing all numeric values. Given a number N which is in decimal representation. our task is to convert the decimal representation of the number to i
5 min read
JavaScript Program for Decimal to any base conversion
In this JavaScript article, we will see how we can do decimal to any base conversion in JavaScript. The base can not be less than 2 and can not exceed 36, So we always have to find out the base of a decimal that lies in between this range, which is '2=< base <=36'. Example: Input: number = "11
5 min read
Javascript Program to Rotate digits of a given number by K
Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
PHP Program to Reverse Bit of a Number
Given a number N, the task is to Reverse the Bit of the number and convert back into a decimal number in PHP. Examples: Input: N = 11Output: 13Explanations: (11)10 = (1011)2 After revering the Bits (1101)2 = (13)10Approach 1: Reverse Bit of a Number using Bitwise Shift OperatorsIn this approach, we
2 min read
Javascript Program to Generate all rotations of a number
Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31
3 min read
Write a program to display Reverse of any number in PHP ?
Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by
2 min read