The padStart(length [, padString])

ES2017 (ES8) provides the padStart() method, which pads the given string with another given string to make the original string of the required length. The padding is done from the start of the string.

If no padString is passed, spaces are assumed by default. Take a look at the following examples:

'normal'.padStart(7);'1'.padStart(3, '0');'My Awesome String'.padStart(20, '*');''.padStart(10, '*');'Hey!'.padStart(13, 'But this is long');

The outputs of each line will be:

" normal""001""****My Awesome String""**********""But this Hey!"

Note that the length supplied in the padStart function will be the maximum length of the whole string. If the original string is already larger than the padStart supplied length, ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.