Computer >> Computer tutorials >  >> Programming >> Javascript

How to preserve leading 0 with JavaScript numbers?


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 −

<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>