• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Reverse a Number

Using JavaScript to Reverse a Number

July 6, 2022 Leave a Comment

We can use JavaScript to reverse a number easily by using converting the number into a string and then applying some JavaScript methods to reverse the string before we convert the string back to a number. We will make use of the JavaScript split(), reverse() and join() methods. Below is a function we will create to reverse a number.

function reverseNumber(num){
  var stringNum = num + "";
  var newStringArr = stringNum.split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  var backToNumber = Number(newStringArr);
  return backToNumber;
};

And now we can simplify the function a little bit and give it a number to test with:

function reverseNumber(num){
  var newStringArr = (num+"").split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return Number(newStringArr);
};

console.log(reverseNumber(456));

#Output:
654

So to recap, to reverse a number, we first have to convert it to a string. We can then use the split() method to get an array of each character in the string, and then use the reverse() method to return the array with all of the characters in reverse.

After reversing the array we then join the characters back together using the JavaScript join() method.

We finally convert the string back to a number using the Number() method.

Below is our function once again on how to reverse a number using JavaScript.

function reverseNumber(num){
  var newStringArr = (num+"").split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return Number(newStringArr);
};

Note that if we tried to reverse a number like 100, we would get the follwoing:

function reverseNumber(num){
  var newStringArr = (num+"").split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return Number(newStringArr);
};

console.log(reverseNumber(100));

#Output:
1

This happens because when we convert the String back to a Number, “001” is just the number 1. If we wanted to reverse a number with all of the digits included, we would have to return it as a string and not convert it to a number at the end.

Here would be the code to do that:

function reverseNumber(num){
  var newStringArr = (num+"").split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return newStringArr;
};

console.log(reverseNumber(100));

#Output:
001

Hopefully this article has been helpful for you to learn how to use JavaScript to reverse a number.

Other Articles You'll Also Like:

  • 1.  Remove Commas From Array in JavaScript
  • 2.  Using JavaScript to Check if Variable Exists
  • 3.  Using JavaScript to Get the Decimal Part of a Number
  • 4.  Using JavaScript to Get the Current URL
  • 5.  JavaScript Round to Nearest 10
  • 6.  Using JavaScript to Run a Function Every 5 seconds
  • 7.  How to Create a New h1 Element With JavaScript
  • 8.  Using JavaScript to Set Select to the First Option
  • 9.  JavaScript isPrime – How to Check if a Number is Prime in JavaScript
  • 10.  Finding the Length of a String in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy