- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse a number in JavaScript
Our aim is to write a JavaScript function that takes in a number and returns its reversed number
For example, reverse of 678 −
876
Here’s the code to reverse a number in JavaScript −
Example
const num = 124323; const reverse = (num) => parseInt(String(num) .split("") .reverse() .join(""), 10); console.log(reverse(num));
Output
Output in the console will be −
323421
Explanation
- Let’s say num = 123
- We convert the num to string → num becomes ‘123’
- We split ‘123’ → it becomes [‘1’, ‘2’, ‘3’]
- We reverse the array → it becomes [‘3’, ‘2’, ‘1’]
- We join the array to form a string → it becomes ‘321’
- Lastly we parse the string into a Int and returns it → 321
- Related Articles
- Reverse a Number in PL/SQL
- How to reverse a number in Python?
- Reverse a number using stack in C++
- Array reverse() in JavaScript
- Reverse numbers in function without using reverse() method in JavaScript
- C++ Program to Reverse a Number
- Swift Program to Reverse a Number
- Java Program to Reverse a Number
- Kotlin Program to Reverse a Number
- JavaScript Array reverse()
- Function to reverse a string JavaScript
- Write a Golang program to reverse a number
- How to create reverse of a number in R?
- Reverse sum array JavaScript
- Write a program to reverse an array in JavaScript?

Advertisements