New String Extensions

padding in strings for alignment

This lesson is about two String prototype extensions:

These two methods are not yet implemented in all browsers. You need to open the Firefox developer tools or Chrome 57 to experiment with them.

Padding is used to add additional characters to the start or the end of a string so that it reaches a given size.

Padding is useful in character mode for alignment.

In the following example, let’s format the amounts such that the integer part contains six characters, and the fractional part contains two characters. Let’s pad the characters in front of the integer part with spaces, and the decimal part with zeros.

Let’s console log the result.

Get hands-on with 1400+ tech skills courses.