Blog / 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
August 26, 2021
Article

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``

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``

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"``

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.

Summary

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!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

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