Remove the leading Zeros from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Table of Contents

  1. Remove the leading Zeros from a String in JavaScript
  2. Remove the leading Zeros from a String using parseFloat()
  3. Remove the leading Zeros from a String using replace()
  4. Remove the leading Zeros from a Number using the Number() constructor
  5. Remove the leading zeros from a Number by multiplying by 1
  6. Remove the leading Zeros from a Number using the unary plus (+)

# Remove the leading Zeros from a String in JavaScript

To remove the leading zeros from a string, call the parseInt() function, passing it the number and 10 as parameters, e.g. parseInt(num, 10).

The parseInt function parses a string argument and returns a number with the leading zeros removed.

index.js
const num = '00123'; const withoutLeading0 = parseInt(num, 10); console.log(withoutLeading0); // ๐Ÿ‘‰๏ธ 123

remove leading zeros from string

The code for this article is available on GitHub

We passed the following arguments to the parseInt() function:

  1. The value to parse.
  2. The radix - for our purposes, it should be 10. However, it doesn't default to 10, so make sure to specify it.
The parseInt() function returns an integer that is parsed from the given string.

This approach would also work if we had non-digit characters at the end of the string.

index.js
const num = '00123bobby_hadz_com'; const withoutLeading0 = parseInt(num, 10); console.log(withoutLeading0); // ๐Ÿ‘‰๏ธ 123

# Remove the leading Zeros from a String using parseFloat()

If you need to remove the leading zeros from a string and get a floating-point number, use the parseFloat() function instead.

index.js
const num = '00012.34'; const result = parseFloat(num); console.log(result); // ๐Ÿ‘‰๏ธ 12.34

remove leading zeros from string using parsefloat

The code for this article is available on GitHub

The parseFloat() function parses a string argument and returns a floating-point number.

The function also handles the scenario where the string contains non-digit characters at the end.

index.js
const num = '00012.34bobby_hadz_com'; const result = parseFloat(num); console.log(result); // ๐Ÿ‘‰๏ธ 12.34

# Remove the leading Zeros from a String using replace()

You can also use the replace() method to remove the leading zeros from a number.

index.js
const num = '00012.34'; const result = num.replace(/^0+/, ''); console.log(result); // ๐Ÿ‘‰๏ธ 12.34

remove leading zeros from string using replace

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The first argument we passed to the String.replace() method is a regular expression.

index.js
const num = '0001234'; const result = num.replace(/^0+/, ''); console.log(result); // ๐Ÿ‘‰๏ธ "1234"

The forward slashes / / mark the beginning and end of the regex.

The caret ^ matches the beginning of the input.

The plus + matches the preceding item (a zero) 1 or more times.

In its entirety, the replace() method matches one or more zeros at the start of the string and replaces the match with an empty string.

This approach also works for floating-point numbers.

index.js
const num = '00012.34'; const result = num.replace(/^0+/, ''); console.log(result); // ๐Ÿ‘‰๏ธ "12.34"

Note that the replace() method returns a string.

If you need to convert the result to an integer or a floating-point number, use the parseInt() or parseFloat() functions.

index.js
const num = '0001234'; const str = num.replace(/^0+/, ''); console.log(str); // ๐Ÿ‘‰๏ธ "1234" const result = parseInt(str, 10); console.log(result); // ๐Ÿ‘‰๏ธ 1234

# Remove the leading Zeros from a Number using the Number() constructor

You can also use the Number() constructor to remove the leading zeros from a string.

index.js
const str = '0001234'; const result = Number(str); console.log(result); // ๐Ÿ‘‰๏ธ 1234

remove leading zeros from number using number constructor

The code for this article is available on GitHub

The Number() constructor converts the given value to a number which automatically drops all leading zeros.

# Remove the leading zeros from a Number by multiplying by 1

When you multiply a string by 1, it implicitly gets converted to a number.

index.js
const str = '0001234'; const result = str * 1; console.log(result); // ๐Ÿ‘‰๏ธ 1234

You can imagine that the string got converted to a number implicitly and got multiplied by 1.

Note that NaN will be returned if your string contains non-digit characters.

index.js
const str = '0001234_abc'; const result = str * 1; console.log(result); // ๐Ÿ‘‰๏ธ NaN

# Remove the leading Zeros from a Number using the unary plus (+)

The unary plus operator attempts to convert the value to a number which removes all leading zeros.

index.js
const num = '00123'; const withoutLeading0 = +num; console.log(withoutLeading0); // ๐Ÿ‘‰๏ธ 123
The code for this article is available on GitHub

You can think of the unary plus (+) operator as an attempt to convert the value to a number.

If the value can be converted to a number, all leading zeros are dropped.

However, if it cannot be converted to a number, the operator returns NaN.

index.js
const num = '00123bobby_hadz_com'; const withoutLeading0 = +num; console.log(withoutLeading0); // ๐Ÿ‘‰๏ธ NaN

The string from the example cannot directly be converted to a number, so the unary plus (+) operator returns NaN.

If you have to handle this scenario, use the parseInt() function instead.

index.js
const num = '00123bobby_hadz_com'; const withoutLeading0 = parseInt(num, 10); console.log(withoutLeading0); // ๐Ÿ‘‰๏ธ 123

I've also written an article on how to remove the trailing zeros from a number.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev