OneCompiler

[Javascript] How to pad a string with a given character to specific length?

I want to pad a string with a given character for specific length, How can I do that in Javascript?

1 Answer

4 years ago by

.padStart() and .padEnd() can be used based on your requirement to pad a string to specific length. check the below example on usage.

let str = "Hello World";

console.log(str.padStart(30, '.'))
console.log(str.padEnd(30, '.'))

Run here https://onecompiler.com/javascript/3xnrs4juy

4 years ago by Meera