Javascript String padEnd()

Introduction

The padEnd() method appends string to the end to make it certain length.

The first argument is the desired length, and the second is the optional string to add as a pad.

str.padEnd(targetLength [, padString])
  • targetLength - the length of the resulting string.
  • padString - Optional, the string to pad
console.log('abc'.padEnd(10));          // "abc ? ? ? ? ? ? "
console.log('abc'.padEnd(10, "foo"));   // "abcfoofoof"
console.log('abc'.padEnd(6, "123456")); // "abc123"
console.log('abc'.padEnd(1));           // "abc"

If not provided, the U+0020 'space' character will be used.

let stringValue = "foo"; 
console.log(stringValue.padEnd(6));         // "foo   " 
console.log(stringValue.padEnd(9, "."));    // "foo......" 

The optional argument is not limited to a single character.

let stringValue = "foo"; 

console.log(stringValue.padEnd(8, "bar"));    // "foobarba"  
console.log(stringValue.padEnd(2));           // "foo" 



PreviousNext

Related