Last updated: Mar 3, 2024
Reading timeยท4 min

To add leading zeros to a number:
String() object to convert the number to a string.padStart() method to add zeros to the start of the string.padStart method will return a new string, padded with leading zeros.function padWithLeadingZeros(num, totalLength) { return String(num).padStart(totalLength, '0'); } console.log(padWithLeadingZeros(3, 2)); // ๐๏ธ "03" console.log(padWithLeadingZeros(3, 3)); // ๐๏ธ "003" console.log(padWithLeadingZeros(3, 4)); // ๐๏ธ "0003" console.log(padWithLeadingZeros(100, 2)); // ๐๏ธ "100" // ๐๏ธ Alternatively, simply use the Addition (+) operator const num = '00' + 3; console.log(num); // ๐๏ธ "003"

The padWithLeadingZeros() function takes a number and its desired total length
as parameters and pads the number with leading zeros if necessary.
The String.padStart() method pads the current string with the provided string until the resulting string reaches the given length.
The padStart method takes the following 2 arguments:
| Name | Description |
|---|---|
targetLength | The string gets padded with the pad string up to this length. |
padStart | The string to pad the current string with. |
The targetLength argument also takes into consideration decimal characters or
a minus sign.
An easy way to think about the method is that it pads the original string to a target length with a supplied string.
const num = 3; console.log(String(num).padStart(2, '0')); // ๐๏ธ 03 console.log(String(num).padStart(3, '0')); // ๐๏ธ 003 console.log(String(num).padStart(4, '0')); // ๐๏ธ 0003
If you have a string of length 2 and you set the target length argument to
4, the string will get padded with 2 leading zeros.
// ๐๏ธ "0022" console.log(String(22).padStart(4, '0'));
Make sure to convert the number to a string before using the padStart()
method.
const num = 123; const str = String(num); // โ pad number with 2 leading zeros console.log(str.padStart(str.length + 2, '0')); // โ pad number with 3 leading zeros console.log(str.padStart(str.length + 3, '0'));
By adding n to the length of the number to determine the target length, we
always add n leading zeros to the number.
If you convert the padded string back to a number, any of the leading zeros will automatically get dropped.
const num = 3; const result = Number(String(num).padStart(5, '0')); console.log(result); // ๐๏ธ 3
If the length of the number exceeds the provided target length, the entire
string gets returned from the padStart method.
function padWithLeadingZeros(num, totalLength) { return String(num).padStart(totalLength, '0'); } const num = 123456; console.log(padWithLeadingZeros(num, 3)); // ๐๏ธ "123456"
If you need to handle negative numbers, you need to add an if statement that
adds the minus sign after the leading zeros have been added.
function addLeadingZeros(num, totalLength) { if (num < 0) { const withoutMinus = String(num).slice(1); return '-' + withoutMinus.padStart(totalLength, '0'); } return String(num).padStart(totalLength, '0'); } console.log(addLeadingZeros(3, 2)); // ๐๏ธ "03" console.log(addLeadingZeros(-3, 3)); // ๐๏ธ "-003"

We added an if statement to check if a negative number is provided to the
function.
Note that we deliberately don't include the minus sign in the target length for the new string.
The addition (+) operator will convert the number to a string and will add the specified number of leading zeros to the beginning of the string.
const positive = '00' + 5; console.log(positive); // ๐๏ธ "005" const negative = '-' + '00' + String(-5).slice(1); console.log(negative); // ๐๏ธ "-005"

When using the addition operator with a number and a string, the number gets converted to a string and the two strings get concatenated.
We used the same approach to handle negative numbers as we did with the
padStart method.
If you have to pad a number with leading zeros often, define a reusable function.
function padWithLeadingZeros(num, n) { const char = '0'; return char.repeat(n) + num; } const num = 123; console.log(padWithLeadingZeros(num, 2)); // ๐๏ธ 00123 console.log(padWithLeadingZeros(num, 3)); // ๐๏ธ 000123 console.log(padWithLeadingZeros(num, 4)); // ๐๏ธ 0000123
The padWithLeadingZeros function takes a number and how many leading zeros
should be added to the number as parameters.
The function uses the
String.repeat() method to
repeat the zero n times and adds it to the beginning of the string.
console.log('0'.repeat(3)); // ๐๏ธ "000" console.log('0'.repeat(2)); // ๐๏ธ "00"
You can also use a while loop to add leading zeros to a number.
while loopThis is a three-step process:
while loop to iterate for as long as the string hasn't reached the
target length.function padWithZero(num, targetLength) { let str = String(num) while (str.length < targetLength) { str = '0' + str } return str } const num = 5; // โ pad with 2 leading zeros console.log(padWithZero(num, String(num).length + 2)); // ๐๏ธ '005' // โ pad with 3 leading zeros console.log(padWithZero(num, String(num).length + 3)); // ๐๏ธ '0005' // โ pad with 4 leading zeros console.log(padWithZero(num, String(num).length + 4)); // ๐๏ธ '00005'
The padWithZero() function is very similar to the padStart method.
It takes a number and the desired target length as parameters and pads the number with leading zeros to the specified length.
The first step is to convert the number to a string.
The while loop iterates for as long as the string's length is less than the
desired target length.
On each iteration, we use the addition (+) operator to add a leading zero to the string.
Once the string reaches the desired target length, the condition in the while
loop is no longer met.