You can reverse a number in JavaScript by converting it to a string, using the split() method to create an array of characters, reversing the order of the elements in the array using the reverse() method, and then joining the characters back together into a string using the join() method. Finally, convert the reversed string back to a number using the parseInt() function or Number() constructor.

Here is an example of how to reverse a number in JavaScript:

function reverseNumber(num) {
  return parseInt(num.toString().split('').reverse().join(''));
}
console.log(reverseNumber(12345));  // Output: 54321

You can also use bit manipulation operator to reverse a number

function reverseNumber(num) {
  let rev = 0;
  while (num > 0) {
    rev = (rev * 10) + (num % 10);
    num = Math.floor(num / 10);
  }
  return rev;
}
console.log(reverseNumber(12345));  // Output: 54321

Also Read:

Categorized in: