How to use String Iteration In Javascript
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
JavaScript Program to Reverse Digits of a Number
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.
There are several methods that can be used to Reverse the Numbers by using JavaScript, which is listed below:
Table of Content
- Using String Reversal
- Using Array Reduce() Method
- Using String Iteration
- Using Recursion
Contact Us