Explaining padStart & padEnd – two ES8 string methods

EcmaScript 2017 or ES8 has introduced two new String prototype methods. padStart and padEnd. Both of them will be used in display related purpose. Even the name says it’s all about “padding”.
You can add some extra spaces or dashes (or any other character), before of after a string.
The specs are still not finalized, so there could be some changes in the signature or contract; however chances are less.

padStart

str.padStart(targetLength, padString);

padEnd

str.padEnd(maxLength, padString);

How does it work

  • You can specify the final length or maxLength of the string.
  • To achieve that maxLength it adds the padString repeatedly to the start or the end of the given string.
  • The default pasString is a space character.
  • If maxLength is lesser than the string length, it does nothing. It will not shorten the string, but will return the unmodified one.
About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.