In JavaScript, there are a couple of ways that we can remove leading zeros from a string. The easiest way is to simply use the JavaScript parseInt() method.
var num = '0000123';
var newNum = parseInt(num, 10);
And that’s it. We can wrap our code in a simple function so that we can remove the leading zeroes from any string.
function removeZeros(num){
return parseInt(num, 10);
};
And finally, let’s use this function on a string to see how it works:
function removeZeros(num){
return parseInt(num, 10);
};
console.log(removeZeros("00001234"));
#Output:
1234
When working with strings, the ability to easily be able to manipulate and change the values of those strings is valuable.
One such case is if you have leading zeros in a string that represents an ID or a record number and you want to get rid of those zeros.
To remove leading zeros from a string in JavaScript, the easiest way is to use the JavaScript parseInt() method.
Below is our function again to remove leading zeros from a string with a few more string examples:
function removeZeros(num){
return parseInt(num, 10);
};
console.log(removeZeros("00001234"));
console.log(removeZeros("0012003456"));
console.log(removeZeros("000012340000"));
console.log(removeZeros("00Hello120"));
#Output:
1234
12003456
12340000
0
As you can see in the last example, when the string we enter contains a character that is not a number, removing the leading zeros does not work the way we want.
So to remove leading zeros from any string, we can use a loop.
Removing Leading Zeros from a String in JavaScript with a While Loop and Substring Method
Another way we can remove leading zeros from a string variable in our JavaScript code is with a while loop and the substring() method.
The idea here is that you loop until the first character is not a zero. If it is a zero, then you want to remove the first character from the string.
Below is an example of how to use a while loop and the substring() method to remove the leading zeros from a string in JavaScript. We will also have to make use of the charAt() method to get the first character of the string.
var num = "00001234";
while (num.charAt(0) == "0"){
num = num.substring(1);
}
Once again, we can wrap our code in a simple function so that we can remove the leading zeroes from any string.
function removeZeros(num){
while (num.charAt(0) == "0"){
num = num.substring(1);
}
return num;
};
And finally, let’s use this function on some strings to see how it works:
function removeZeros(num){
while (num.charAt(0) == "0"){
num = num.substring(1);
}
return num;
};
console.log(removeZeros("00001234"));
console.log(removeZeros("0012003456"));
console.log(removeZeros("000012340000"));
console.log(removeZeros("00Hello120"));
#Output:
1234
12003456
12340000
Hello120
As you can see in the last example, this time the leading zeros are removed when our string contains something other than numbers.
Hopefully this article has been useful for you to learn how to use JavaScript to remove leading zeros.