How to trim and remove leading zeros in JavaScript

How to trim and remove leading zeros in JavaScript

In this post find out about how to trim and remove leading zeros in JavaScript using parseInt, the addition operator and regex

Will Mayger

A post by Will Mayger
August 26, 2021

In this post we will be covering how to trim and remove leading zeros in JavaScript with as little technical jargon as possible so you will have everything you need right here without having to look any further!

You might find yourself needing to remove or trim leading zeros in JavaScript, whilst this probably won’t come up all that often, it could be frustrating.

Either way, there are a few easy fixes for this problem that we will cover here.

This post assumes that your number is in the form of a string and we will look into how to trim and remove leading zeros from a string in JavaScript.

How to trim and remove leading zeros in JavaScript

The easiest way to trim and remove leading zeros in JavaScript is to just use the parseInt method.

Using parseInt works because leading zeros in an integer do not create a valid integer so when you use parseInt it will remove any leading zeros because they don’t change the number in any way.

Here is an example of how you can use parseInt to tram/remove leading zeros in JavaScript:

parseInt("0400", 10) // 400
parseint remove leading zeros

In the above example, the second argument of 10 is known as the radix which is the numerical base which the parseInt method will use to parse the correct integer.

For example if you pass in 2 as the base/radix then you would be working with binary numbers, and if you use 10 you will be using the normal mathematical system that we all know and use.

The reason why you will want to pass in the radix is because it does not default to 10 like you would expect, if you want to read more about this I suggest taking a look at this post about parseInt.

Most of the time if you have a leading 0 it will be because the number is either in string format or it is from a user input.

Another way you can remove any leading zeros in a number that is currently a string is to use the addition operator which will then cause the string to act as if a number which will convert it into a number in JavaScript.

Here is how you can do this:

+"0400" // 400
Addition operator to remove leading zero

This will not work if the number is not in a string format though.

The last option would be to use a regular expression/regex to remove the first 0 in a string, but this is far from ideal because it could become confusing to write and read.

Here is how that would look:

"0400".replace(/^0+/, "") // "400"
regex remove leading zeros

The best option to be able to trim or remove leading zeros in JavaScript is the parseInt method whilst proving the correct radix like in the example above.


There we have how to trim and remove leading zeros in JavaScript, if you want more like this be sure to check out some of my other posts!

I hope this post has helped, but before you go I highly recommend that you checkout Pluralsight if you are interested in improving your software engineering skills, such as React, JavaScript, or Typescript (and much more).

Not only does Pluralsight provide you with all the learning material you need but it also let's you learn in a fun, interactive way and gives you the ability to see your current progress and level in a certain skill via their Skill Assessment tool which can help you see what you need to learn next to keep improving!

Some graphics used on this post were made using icons from flaticon.