7 examples of 'javascript add leading zeros' in JavaScript

Every line of 'javascript add leading zeros' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
30function addZeros (block) {
31 if (block === '') {
32 return '0';
33 } else {
34 return block;
35 }
36}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4function addZeros(i, zeros) {
5 if (zeros === undefined) {
6 zeros = 3;
7 }
8 var s = i.toString();
9 while (s.length < zeros)
10 {
11 s = '0' + s;
12 }
13 return s;
14}
49function addZeros(value, len) {
50 value = "" + value;
51
52 if (value.length < len) {
53 for (var i=0; i<(len-value.length); i++)
54 value = "0" + value;
55 }
56
57 return value;
58};
5function addZeros(n, l){
6 n = n.toString();
7 while(n.length < l){
8 n = '0' + n;
9 }
10 return n;
11}
6function addZeros(str, targetLength = 2) {
7 while (str.length < targetLength) {
8 str = `0${str}`
9 }
10
11 return str
12}
150function addLeadingZero(input, index) {
151 return input.slice(0, index) + '0' + input.slice(index);
152}
17function addLeadingZeroes(num) {
18 return (num > 9 ? '' : '0') + num;
19}

Related snippets