JavaScript

How to Output Numbers With Leading Zeros in JavaScript

Any number having 0’s before the non-zero digit or a string is known as a leading zero. For instance, 008, 05, 000XXX, and so on. Sometimes, developers must show errors during testing with leading zeros strings or numbers. JavaScript allows performing the mentioned operation using the addition (+) operator or the padStart() method.

This blog will describe the methods to get the numbers with leading zeros in JavaScript.

How to Output Numbers With Leading Zeros in JavaScript?

To get the numbers with leading zeros, use the given methods:

Method 1: Output Numbers With Leading Zeros Using Addition Operator

The addition operator “+” is used for concatenating numbers or strings. It can also be used to concatenate the number with leading zeros.

Syntax

Follow the provided syntax for using the addition operator:

var number = "zeros" + number

Example

Create a variable “numLeadingZeros” and concatenate the number with zeros using the addition operator:

var numLeadingZeros = "00" + 5;

Print the concatenated number on the console:

console.log("Number with leading zeros: " + numLeadingZeros);

As you can see the output displays a number with leading zeros:

Note: If you want to add leading zeros with the negative number then use the “slice()” method with the addition operator.

First, use the slice() method to cut the (-) sign from the string. Then, add the zeros to the number. After that, again add the (-) sign with the string using the (+) operator:

const numLeadingZeros = '-' + '00' + String(-3).slice(1);

Print the number on the console:

console.log("Number with leading zeros: " + numLeadingZeros);

The output indicates that the negative number is successfully concatenated with leading zeros:

Method 2: Output Numbers With Leading Zeros Using padStart() Method

To get the number with leading zeros, use the JavaScript “padStrat()” method. This method pads/joins the current string with another string (as often as necessary) until the resultant string is the specified length. The padding is added from the beginning of the given string.

Syntax

Use the below-given syntax for padding the strings using the padString() method:

padStart(targetLength, padString)

Here:

  • targetLength” is the final length of the resultant string after padding.
  • padString” is the string that will pad with the given string.

Example

First, create a variable “numLeadingZeros” and store a number:

var numLeadingZeros = '5';

Then, call the “padStart()” method by passing the total length of the resultant string that is “3” and the padString “0” that will be padded with the given string “5”:

console.log("Number with leading zeros: " + numLeadingZeros.padStart(3, '0'));

Output

We have provided all the essential information related to getting the number with leading zeros.

Conclusion

For adding and getting the number with leading zeros, use the addition operator (+) or the JavaScript “padStart()” method. For the negative numbers, use the “slice()” method with the addition operator (+). This blog describes the methods to get the numbers with leading zeros in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.