javascript remove leading zeros from string 1

javascript remove leading zeros from string

use a regular expression like this  

var strNumber = "0049";  
console.log(typeof strNumber); //string  
  
var number  = strNumber.replace(/^0+/, ''); 

Here is what the above code is Doing:
1. The regular expression /^0+/ matches any string that starts with one or more zeros.
2. The replace() method replaces the matched string with an empty string.
3. The result is a string that has all leading zeros removed.

Similar Posts