JavaScript

How to Remove Leading Zeros from a String in JavaScript

When a string contains extra leading zeros, it can be very irritating. Moreover, sometimes, it becomes important to remove them so that the string can be processed further. The removal of leading zeros from a string can be a little challenging task for beginners, but no worries!

This write-up will demonstrate the methods to eliminate leading zeros from a string.

How to Remove Leading Zeros from a String in JavaScript?

For removing leading zeros from a string, utilize the methods listed below:

Let’s examine the working of each method separately.

Method 1: Remove Leading Zeros from a String Using parseInt() Method

The “parseInt()” method is used for removing the leading zeros from a String containing integer values. It accepts a string as an argument and gives it as output after removing the leading zeros.

Syntax
Follow the given syntax to use the parseInt() method:

parseInt(strng);

Have a look at the following example to know more about the given method.

Example
First, we will create a string that contains numbers starting with zeros:

var strng = "0010100";

Then, we will call the parseInt() method to remove the leading zeros by passing the string to the method and store it in a variable named “numb”:

var numb = parseInt(strng);

Finally, print the returned value on the console:

console.log(numb);

The given output indicates that the leading zeros are successfully removed from the string:

If you want to remove the leading zeros from a floating point number in a string, see the next given section.

Method 2: Remove Leading Zeros from a String Using parseFloat() Method

For removing leading zeros from a string that contains a floating point number, use the “parseFloat()” method. It also accepts a string as an argument.

Syntax
Follow the below syntax to use the parseFloat() method:

parseFloat(strng);

Example
Here, we will create a string containing the decimal number leading to zeros:

var strng = "001.250";

Then, call the parseFloat() method by passing the string in it as an argument and stores the resultant value in variable “numb”:

var numb = parseFloat(strng);

Finally, we will print the resultant value on the console using “console.log()” method:

console.log(numb);

As you can see that the given output shows that the zeroes leading from the string are successfully removed:

Let’s head towards the next method!

Method 3: Remove Leading Zeros from a String Using replace() Method

You can use the “replace()” method to eliminate leading zeros from a string. This method uses the pattern or regex that helps to replace something based on that regex.

Syntax
The following described method is used for the replace() method:

strng.replace(regex, replacer);

Here, “regex” is the pattern that helps to replace something with “replacer”.

Example
In this example, first we will create a string named “strng” and assign it a value having leading zeros:

var strng = "001500";

Then, we will call the replace() method by passing two parameters; the regex, which is used to remove leading zeros from a string and the empty string that acts as a replacer:

var numb = strng.replace(/^0+/, '');

Finally, we will print the resultant value using the “console.log()” method:

console.log(numb);

Output

Let’s move ahead to understand some other methods for removing leading zeros from the string.

Method 4: Remove Leading Zeros from a String Using Number Constructor

The Number() constructor takes a string as an argument and returns the number by truncating zero from the start of the string.

Syntax
Use the following syntax to utilize the Number constructor:

Number(strng);

Example
We will create a new string named “strng” and assign value “000100”:

var strng = "000100";

Then, pass the string to the Number constructor which will remove the starting zeros from the string:

var numb = Number(strng);

Print the resultant value that is stored in the variable “numb”:

console.log(numb);

As you can see in the output, the starting zeros are removed from the string:

Let’s see another approach for removing zeros from a string.

Method 5: Remove Leading Zeros from a String by Multiplying it with 1

You can remove zeros placed before the number by multiplying the string with “1”. When the string multiplies with one, it removes the starting zeros and returns the actual number.

Syntax
Follow the given syntax for deleting the leading zeros from a string:

strng * 1;

Example
We will create a string “strng” with value “001020”:

var strng = "001020";

Then, multiply the string with one and store it in a variable “numb”:

var numb = strng * 1;

Print the resultant value using “console.log()” method:

console.log(numb);

Output

We have gathered different methods for removing leading zeros from the string.

Conclusion

For removing leading zeros from a string, you can use parseInt() method, parseFloat() method, replace() method, Number() constructor, or multiply the string by 1. The parseInt() method returns the int value by removing leading zeros, while the parseFloat() method gives the floating-point number by truncating the leading zeros. The replace() method uses regex to remove zeros. In this write-up, we have demonstrated various methods for removing leading zeros from a string.

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.