All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Reverse Number In JavaScript

Last Updated : Mar 11, 2024

Reverse Number In JavaScript

In this tutorial we will show you the solution of reverse number in JavaScript, we can had many options for that here we used methods of ‘Number(),split(),reverse(),join()’.

Those are doing their work properly then printed on console using console.log() method.

We splitting each value from whole integer then reversely joined with one with another and whole string changed to integer type using number method finally we gets result.

Step By Step Guide On Reverse Number In JavaScript :-

Here we used methods of ‘Number(),split(),reverse(),join()’ for reverse a integer value.

The split() to split the characters of the string and added them to an array and method reverse() to reverse the array.

The join() method joins all characters to a string and Number() return string as a whole integer with reverse order. Using this concept we can reverse any integer dynamically also.

<!DOCTYPE html>
<html>
    <head>
        <title>Reverse Number</title>
    </head>
    <body>
        <script>
            let rev=0;
            let num=45678;
            rev=Number(String(num).split('').reverse().join(''));
            console.log("Reverse Number :"+rev);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Within <script> tag we defined our javascript program, here we initialized variables ‘rev,num’ with respective values ‘0,45678’.
  7. Here first string() method used to converts integer as string value then split() method appends with that ‘.’. So result of converted string ‘45678’ splited as characters and stored to array.
  8. This method appends with reverse() method so result of characters array reversed and stored as array that is appends with join() method.
  9. It is converts array each characters to a string then result will finally passed to Number() method for getting as whole digit integer value from a string and stored to variable ‘rev’.
  10. Printed result using ‘console.log()’ method. Console.log() is a function in javascript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Its output display on the javascript console.
  11. In browser for open javascript console use the shortcut ‘ctrl+shift+j’ then console panel will open at right hand side there we can see our result.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to reverse number in javascript.

When we execute our program on browser we can’t see the result so we have to use that shortcut ‘ctrl+shift+j’ then result will see printed on console by console.log().

We can also reverse any numbers by looping, result will be same.

The console.log() method only will print any message or value on the console, for general printing values or string on webpage in javascript we had many inbuilt methods.

I hope this tutorial on reverse number in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪