Javascript - String padding Methods


The padStart() and padEnd() method are two new string methods introduced in ES2017.

You can add padding to a string so that the resulting string is of the length passed into them as the first parameter.

You can pass in a second optional parameter of another string you can use to pad instead of the default space.


console.log('string'.padStart(10)); // "    string" 
console.log('string'.padStart(10, "abc"));"abcastring" 
console.log('string'.padStart(10,"123465"));// "1234string" 
console.log('string'.padStart(8, "0"));// "00string" 
console.log('string'.padStart(3));// "string" 
console.log('string'.padEnd(10));// "string    " 
console.log('string'.padEnd(10, "abc"));// "stringabca" 
console.log('string'.padEnd(8, "123456"));// "string12" 
console.log('string'.padEnd(1));// "string"