How to use padStart and padEnd in JavaScript

Photo by Emre Turkan on Unsplash

How to use padStart and padEnd in JavaScript

You can use the padStart() and padEnd() string methods to pad a string on the left and right, respectively. Both methods take two arguments: the first argument is the length of the final padded string, and the second argument is the padding character. Here's an example:

In this example, str is the original string, and paddedStr is the padded string with a total length of 10 characters padded on the left with -.

Similarly, you can pad the string on the right using the padEnd() method:

In this example, paddedStr is the padded string with a total length of 10 characters padded on the right with -.

Thanks for reading and learning!