Last updated: Mar 1, 2024
Reading timeยท4 min
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.
const num = '00123'; const withoutLeading0 = parseInt(num, 10); console.log(withoutLeading0); // ๐๏ธ 123
We passed the following arguments to the parseInt() function:
10
. However, it doesn't default
to 10
, so make sure to specify it.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.
const num = '00123bobby_hadz_com'; const withoutLeading0 = parseInt(num, 10); console.log(withoutLeading0); // ๐๏ธ 123
If you need to remove the leading zeros from a string and get a floating-point
number, use the parseFloat()
function instead.
const num = '00012.34'; const result = parseFloat(num); console.log(result); // ๐๏ธ 12.34
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.
const num = '00012.34bobby_hadz_com'; const result = parseFloat(num); console.log(result); // ๐๏ธ 12.34
You can also use the replace()
method to remove the leading zeros from a
number.
const num = '00012.34'; const result = num.replace(/^0+/, ''); console.log(result); // ๐๏ธ 12.34
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A 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.
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.
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.
const num = '0001234'; const str = num.replace(/^0+/, ''); console.log(str); // ๐๏ธ "1234" const result = parseInt(str, 10); console.log(result); // ๐๏ธ 1234
You can also use the Number()
constructor to remove the leading zeros from a
string.
const str = '0001234'; const result = Number(str); console.log(result); // ๐๏ธ 1234
The Number()
constructor converts the given value to a number which
automatically drops all leading zeros.
When you multiply a string by 1
, it implicitly gets converted to a number.
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.
const str = '0001234_abc'; const result = str * 1; console.log(result); // ๐๏ธ NaN
The unary plus operator attempts to convert the value to a number which removes all leading zeros.
const num = '00123'; const withoutLeading0 = +num; console.log(withoutLeading0); // ๐๏ธ 123
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
.
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.
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.
You can learn more about the related topics by checking out the following tutorials: