Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove leading zeros from a number in JavaScript?
In this tutorial, we will explore how we can remove leading zeroes from any number in JavaScript. JavaScript removes leading zeroes from an integer variable automatically, but this is not the case when we consider strings in JavaScript. It is possible that a string in JavaScript which represents a numerical value may contain leading zeroes.
These leading zeroes may cause problems when we try to perform any operation on the numerical value the string represents. This is why we will explore the ways by which we can remove the leading zeroes from a number, expressed as a string in JavaScript.
Using the parseInt() Method
One of the easiest ways to remove leading zeroes from a number represented as a string in JavaScript is by using the parseInt() method. The parseInt() function in JavaScript takes a string of numerical characters as input and converts it into an integer.
Since a valid integer in JavaScript does not have leading zeroes, when we parse a string using the parseInt() method and convert it into an integer, it will automatically remove any leading zeroes.
Syntax
parseInt(string, radix);
Parameters
string ? The string that is to be converted into an integer. This is a required parameter.
radix ? An optional parameter that denotes the base of the numerical value (2-36). For decimal numbers, use 10.
Example
In the below example, we use parseInt() method to remove the leading zeros from a number (00378).
<!DOCTYPE html>
<html>
<body>
<script>
var num = "00378";
document.write("Original number was: " + num);
var int_num = parseInt(num, 10);
document.write("<br>Parsed number is: " + int_num);
</script>
</body>
</html>
Original number was: 00378 Parsed number is: 378
Using the String replace() Method
JavaScript strings have a method called replace(), which can replace patterns in a string with a replacement value. We can use this method to replace the leading zeroes in our string with an empty string, thus removing them.
Syntax
string.replace(pattern, replacement);
Parameters
pattern ? The substring or regular expression to be replaced. For leading zeros, we can use the regex
/^0+/.replacement ? The string that will replace the pattern. Use an empty string
""to remove.
Example
In the following example, we demonstrate both string replacement and regular expression methods to remove leading zeroes:
<!DOCTYPE html>
<html>
<body>
<script>
var num = "00378";
document.write("Original number was: " + num);
// Using string replacement (removes only exact match)
var replaced_num = num.replace("00", "");
document.write("<br>String replaced number: " + replaced_num);
// Using regex (removes all leading zeros)
var regex_num = num.replace(/^0+/, "");
document.write("<br>Regex replaced number: " + regex_num);
</script>
</body>
</html>
Original number was: 00378 String replaced number: 378 Regex replaced number: 378
Using the Number() Constructor
Another simple approach is to use the Number() constructor, which converts a string to a number and automatically removes leading zeros:
<!DOCTYPE html>
<html>
<body>
<script>
var num = "00378";
document.write("Original: " + num);
var converted = Number(num);
document.write("<br>Using Number(): " + converted);
// Convert back to string if needed
var backToString = converted.toString();
document.write("<br>Back to string: " + backToString);
</script>
</body>
</html>
Original: 00378 Using Number(): 378 Back to string: 378
Comparison of Methods
| Method | Returns | Best For |
|---|---|---|
parseInt() |
Integer | Converting to integer with specific base |
replace() |
String | When you need the result as a string |
Number() |
Number | Simple conversion with decimal support |
Conclusion
We explored three effective methods to remove leading zeros: parseInt() for integer conversion, replace() with regex for string manipulation, and Number() for simple numeric conversion. Choose the method based on whether you need the result as a string or number.
