Prepend a zero in front of any number below 10 - Javascript Number Operation

Javascript examples for Number Operation:Number Format

Description

Prepend a zero in front of any number below 10

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {// ww  w  .j a v a 2  s.  c  o m


function pad(n) {
    if (n < 10)
        return "0" + n;
    return n;
}
console.log(pad(8));
console.log(pad(11));


    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials