DEV Community

ViGnEsH
ViGnEsH

Posted on

How to Reverse a Number in JavaScript with Examples

2026 - April - 05

why used reverse ?

A reverse loop is used process data end to the start, useful for like reversing number .

I will give flowchat


Coding

  1. create 3 veriable
    i.num -> store the give value
    ii.rev -> store the reverse value
    iii.digit-> store the last digit value

  2. next while loop condition num greater than 0 (0 > 0 ) condition fail loop stop

  3. digit num % 10 gets the last digit ,
    why 10 ?
    We use 10 because numbers are in the decimal
    used in divides,multiple..

  4. rev*10 + digit value store in rev

  5. Same time give decimal number can't run
    Modify in without decimal used Math.floor
    num/10 removes last digit

  6. Run repeat in loop fail

  7. Print in Output 3210987

  8. stop entire program

% 10 → last digit
reverse * 10 + digit → build reverse
/ 10 → remove last digit
Enter fullscreen mode Exit fullscreen mode

Top comments (0)