As with any kind of app, JavaScript apps have to be written well otherwise we will run into all kinds of issues later on. In this article, we’ll look at some tips we should follow to write JavaScript code faster and better.

Tip #1: Trim the Leading Zero in a Number String

We can trim the leading zero in a number by parsing the number string to a number.

To parse it into an integer, we can use the parseInt function.

For instance, we can write:

parseInt('010', 10)

Then we’ll parse the number string into a decimal.

If we want to parse a string into a number.

Then we can use the + operator. For instance, we can write:

+'010'

Then we get 10.

Also, we can string the leading 0 with regex and the replace method.

For instance, we can write:

number.replace(/^0+/, '')

^ means the start of the string.

#javascript #web-development

Useful JavaScript Tips — Numbers, Strings, and Strict Mode
1.40 GEEK