• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Remove Leading Zeros

Using JavaScript to Remove Leading Zeros

June 8, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Create a New h1 Element With JavaScript
  • 2.  Using JavaScript to Get the Domain From URL
  • 3.  Using JavaScript to Format the Date in mm dd yyyy
  • 4.  Using JavaScript to Get the Current URL
  • 5.  Using JavaScript to Get URL Hash
  • 6.  Using JavaScript to Declare Multiple Variables
  • 7.  Using JavaScript to Remove Backslash From a String
  • 8.  JavaScript Trig – How to Use Trigonometric Functions in Javascript
  • 9.  setInterval JavaScript – How to Repeat Code in Set Time Intervals
  • 10.  Using JavaScript to Generate a Random Float Between 0 and 1

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy