Reversing a number means to make the digits of the number change places from being first to last or vice versa. JavaScript provides numerous ways to reverse a number. However, to achieve this, we first need to understand how to handle numbers as strings because JavaScript does not provide a straightforward built-in method to reverse a number directly. This article will provide a detailed walkthrough on how to reverse a number in JavaScript, with relevant examples.
The JavaScript toString()
and parseInt()
Methods
In JavaScript, we often work with numbers and strings. JavaScript provides several methods to convert a number into a string and vice versa. `toString()` and `parseInt()` are among the most commonly used methods.
`toString()` is a JavaScript method used to convert a number to a string:
1 2 | let num = 12345; let strNum = num.toString(); // "12345" |
On the other hand, `parseInt()` is a method used to parse a string and return an integer:
1 2 | let strNum = "12345"; let num = parseInt(strNum); // 12345 |
We will use these methods in our approach to reverse a number.
Steps to Reverse a Number in JavaScript
We can reverse a number in JavaScript by following these steps:
- Convert the number into a string using the `toString()` method.
- Split the string into an array of characters using the `split(”)` method.
- Reverse the array using the `reverse()` method.
- Join the reversed array back into a string using the `join(”)` method.
- Convert the string back into an integer using the `parseInt()` method.
Here is an implementation of these steps:
1 2 3 4 5 6 7 8 9 10 | function reverseNumber(num) { let strNum = num.toString(); // step 1 let arrayNum = strNum.split(''); // step 2 let reversedArrayNum = arrayNum.reverse(); // step 3 let reversedStrNum = reversedArrayNum.join(''); // step 4 let reversedNum = parseInt(reversedStrNum); // step 5 return reversedNum; } console.log(reverseNumber(12345)); // output: 54321 |
We can make this code more efficient by chaining all the operations in one line:
1 2 3 4 5 | function reverseNumber(num) { return parseInt(num.toString().split('').reverse().join('')); } console.log(reverseNumber(12345)); // output: 54321 |
Handling Negative Numbers
The above code works well for positive numbers. However, for negative numbers, it will return a positive reversed number due to the `parseInt()` method. Here is how to handle negative numbers:
1 2 3 4 5 6 | function reverseNumber(num) { let reversedNum = parseInt(num.toString().split('').reverse().join('')); return (num < 0) ? -reversedNum : reversedNum; } console.log(reverseNumber(-12345)); // output: -54321 |
In this code, we use a ternary operator to check if the original number was negative. If it was, we return the reversed number as a negative number. Otherwise, we return it as a positive number.
In summary, JavaScript provides several powerful tools and methods for working with numbers and strings. By creatively applying these tools, we can perform complex operations, such as reversing a number.