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.


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

Live Demo

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