Quick Tech Tip – How to reverse a number with JavaScript

In order to reverse a number in JavaScript we first need to convert the number to a string.

The string object is then split into an array of strings. In our case the split(“”) would return [“9”, “8”, “7”, “6”, “5”, “4”, “3”, “2”, “1”].

The reverse() method will reverse the newly split array. [ ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ ]

And finally the join(“”) method will join all the elements of the array into a string.

The result is outputted to the console: 123456789

JavaScript reverse a number

There are other ways to reverse a number in JavaScript and this is just one, and that concludes today’s tech tip.