How to preserve leading 0 with JavaScript numbers?

JavascriptWeb DevelopmentFront End Technology

There’s no way to preserve the leading 0 in JavaScript. However, you can consider this id as a string and pass as an argument.

Example

You can try to run the following code to preserve leading 0 by making it a string −

Live Demo

<html>
   <head>
      <script>
         function sayHello(name, age, id) {
            document.write (name + " is " + age + " years old, with id = "+id);
         }
      </script>
   </head>
   <body>
      <p>Click the following button to call the function</p>
      <form>
         <input type="button" onclick="sayHello('John', 26, '0678')"
            value="Display Employee Info">
      </form>
   </body>
</html>
raja
Published on 03-Feb-2018 13:31:54
Advertisements