Skip to content

Remove leading zeros from a number in JavaScript

How to remove leading zeros from a number like 020?

The simplest way is to use parseInt() with radix 10 when your number is in string format.

Advertisements
parseInt(number, 10)

10 is the decimal base in mathematical numeral systems. Decimal radix removes the leading zeros when parsed.

console.log(parseInt('015', 10)); //15

Another way is to use string replace replace method.

console.log('015'.replace(/^0+/, '')); //15
See also  BSTLevelOrderTraversal - Hackerrank Challenge - Java Solution

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.