JavaScript article thumbnail image.

Do you need to trim the leading zero from a number using JavaScript?

Luckily, JavaScript has some built-in ways to do this. Here are three different ways to trim the leading zero in JavaScript:

  • the parseInt() function
  • a unary operator
  • a regular expression

In this article, we'll walk you through how to use all three of those methods and provide some code samples for you as well.

Let's get started!

Table of Contents


The first method we'll cover is the parseInt() function that parses a string argument and returns an integer with a specified radix (number of unique digits).

As an example, we'll use a string with the value of 058 that we want to convert to a 58 number value.

Here's what the code looks like:

const number = 058

parseInt(number, 10) // 58

We pass both the number variable value and the radix value, which is an integer between 2 and 36 that is the base for the numerical calculations used by the parseInt() function.

If you logged the new value created after applying the parseInt() function, it would be the 58 integer.

In the next section, we'll cover another way to trim that leading zero.

Unary Operator

The second method we'll cover is the + unary operator. Unary operators are operations with only one operand and come before or after the operator.

We'll use the unary plus operator, which will attempt to convert a given string into a number. Since the unary operator doesn't perform any other operations on the string, this is the fastest way to convert something into a number.

Here's what the code looks like:

const number = 058

+number //58

In the process of converting the string into a number, the unary operator will remove any leading zeros attached to the new number it creates.

We'll cover the last method in the next section.

Regular Expression

The third and last method we'll cover is to use a regular expression that takes the string and removes the zero from the beginning.

Here's what the code looks like:

const number = 058

number.replace(/^0+/, "")

The regular expression takes the given string and searches for any zero values in it. If it finds a zero value, it replaces it with an empty space, which effectively removes the value altogether.

If you log the new value, you'll see that the new value is a 58 integer.

That was the last method we'll cover in this article.


There you have it! After this article, you now know how to use JavaScript to remove a leading zero from a number using three different methods.

  • parseInt() function
  • unary operator
  • regular expression

Thanks for reading and happy coding!

About the author

Hi, I'm Nick Major. I'm a software developer from Wisconsin, United States.

I create free and premium courses, have published hundreds of informational coding articles, and am currently a Frontend Software Engineer at RoundingWell.

You can read more about me here.

Picture of Nick Major.