JavaScript | Pad a number with leading zeros
The String prototype property is used to pad a number with leading zeros. This property allows for adding new properties and methods to existing object types.
Syntax:
object.prototype.name = value
Return value: It returns a reference to the String.prototype object.
Example 1: This example adds leading zeros to a number by creating a function and inside this function, if the number is less than the width provided then it adds the leading zeros.
<!DOCTYPE html> <html> <head> <title> JavaScript | Pad a number with leading zeros </title> </head> <body style = "text-align:center;" id = "body"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 16px;"></p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var num = 2213; el_up.innerHTML = 'Number = ' +num; function pad(n, width) { n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n; } function gfg_Run() { el_down.innerHTML = pad(num, 7); } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Example 2: This example adds leading zeros to a number by creating a prototype pad. In this example we can pass a string to the object, whatever we want to pad with the number.
<!DOCTYPE html> <html> <head> <title> JavaScript | Pad a number with leading zeros </title> </head> <body style = "text-align:center;" id = "body"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 16px;"></p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var num = '29213'; el_up.innerHTML = 'Number = ' + num; String.prototype.pad = function(String, len) { var str = this; while (str.length < len) str = String + str; return str; } function gfg_Run() { el_down.innerHTML = num.pad("0", 9); } </script> </body> </html> |
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- Format a number with leading zeros in PHP
- How to remove all leading zeros in a string in PHP ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a float number to the whole number in JavaScript?
- Number.isNaN() In JavaScript
- JavaScript | Number isSafeInteger()
- Javascript | Number() Function
- Factorial of a number using JavaScript
- Check a number is Prime or not using JavaScript
- How to convert a pixel value to a number value using JavaScript ?
- Number Guessing Game using JavaScript
- How to get decimal portion of a number using JavaScript ?
- JavaScript | Number constructor Property
- Number valueOf( ) Method In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


