How to Trim Leading Zeros from a String in JavaScript
We recommend using regular expressions and the string replace()
method to remove leading zeros from a string.
let x = '0042';
x = x.replace(/^0+/, '');
x; // '42';
typeof x; // 'string'
Converting to a Number
You may have seen code that uses x * 1
or +x
to remove leading zeros from a string.
This approach works for basic cases, with the key difference that you end up with a number rather than a string.
let x = '0042';
x = parseInt(x);
x; // 42
typeof x; // 'number'
let y = '007';
y = +y;
y; // 7
typeof y; // 'number'
However, things get tricky with strings that contain hex, octal, and binary literals as shown below.
let x = '0xFF';
x = +x;
x; // 255
Whether this behavior is correct depends on your use case.
However, if you want to treat x
as a string and remove leading zeros, the correct output here would be 'xFF'
.
In that case, using +
or parseInt()
won't work.
You can tell parseInt()
to always use base 10 and avoid parsing strings that start with 0x
as hexadecimal numbers, but then you end up with a 0
.
let x = '0xFF';
x = parseInt(x, 10);
x; // 0, because `parseInt()` parses as much as it can
typeof x; // 'number'
More Fundamentals Tutorials
- How to Add 2 Arrays Together in JavaScript
- The String `match()` Function in JavaScript
- Convert a Set to an Array in JavaScript
- What Does Setting the Length of a JavaScript Array Do?
- Get the Last Element in an Array in JavaScript
- Skip an Index in JavaScript Array map()
- Conditionally Add an Object to an Array in JavaScript