How to reverse a number in JavaScript

By artismarti
Examples using an arrow function and regular JS function
_Photo by [Unsplash](https://unsplash.com/@chuttersnap?utm_source=medium&utm_medium=referral" rel="noopener" target="_blank" title="">chuttersnap on <a href="https://unsplash.com?utm_source=medium&utm_medium=referral" rel="noopener" target="blank" title=")
Reversing a string or reversing a number is one of the common questions asked at programming interviews. Let’s take a look at how this is done.
Rules/Limitations:
- Negative numbers should remain negative.
ex. -12345
becomes -54321
- Any leading zeroes should be removed.
ex. 321000
becomes 123
& not 000123
- The function can accept floats or integers.
ex. 543.2100
becomes 12.345
- The function will return integers as integers.
ex. 54321
becomes 12345
& not 12345.00
Solving with an Arrow Function:
const reversedNum = num => parseFloat(num.toString().split('').reverse().join('')) * Math.sign(num)
Solving with a Regular Function:
function reversedNum(num) {
return (
parseFloat(
num
.toString()
.split('')
.reverse()
.join('')
) * Math.sign(num)
)
}
Difference between an Arrow function & Regular function:
In this example, the only difference between the Arrow Function and the Regular function is that the Regular function needs to provide an explicit return
value.
Arrow functions have an implicit return
value — if they can be written in one line, without the need for the{}
braces.
Let’s break the steps down:
- Convert the number to a string
num.toString()
converts the given number into a String. We do this so we can use the split
function on it next.
let num = -5432100
num.toString()
// num = '-5432100'
- Split the String into an Array
num.split('')
converts the String into an Array of characters. We do this so we can use the Array reverse function (which does not work on a String).
// num = '-5432100'
num.split('')
// num = [ '-', '5', '4', '3', '2', '1', '0', '0' ]
- Reverse the Array
num.reverse()
reverses the order of the items in the array.
// num = [ '-', '5', '4', '3', '2', '1', '0', '0' ]
num.reverse()
// num = [ '0', '0', '1', '2', '3', '4', '5', '-' ]
- Join it back into a string
num.join('')
reassembles the reversed characters into a String.
// num = [ '0', '0', '1', '2', '3', '4', '5', '-' ]
num.join('')
// num = '0012345-'
- Parse the input value into a floating point number:
parseFloat(num)
converts num
into a float from a String.
// num = '0012345-'
parseFloat(num)
// num = 12345
Note: parseFloat
runs in the end (even though it is on the first line of the function) on the reversed number and removes any leading zeroes.
- Multiply it by the sign of the original number — to maintain the negative value.
num * Math.sign(num)
multiplies the number with the sign of the original number provided.
// original value of num = -5432100
// num = 12345
num * Math.sign(-5432100)
// num = -12345
And, there you have it!
Subscribe to my newsletter
Read articles from freeCodeCamp directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

freeCodeCamp
freeCodeCamp
Learn to code. Build projects. Earn certifications—All for free.