ECMAScript 2017 added two String methods: padStart
and padEnd
to support padding at the beginning and at the end of a string.
Example
let str = "5";
str = str.padStart(4,0);
// result is 0005
str = str.padStart(4,0);
// result is 0005
Example
let str = "5";
str = str.padEnd(4,0);
// result is 5000
str = str.padEnd(4,0);
// result is 5000
String Padding is not supported in Internet Explorer.
Firefox and Safari were the first browsers with support for JavaScript string padding:
Chrome 57 | Edge 15 | Firefox 48 | Safari 10 | Opera 44 |
Mar 2017 | Apr 2017 | Aug 2016 | Sep 2016 | Mar 2017 |
Practice Excercise Practice now