JavaScript Code

String padStart() in JavaScript

String padStart()

In JavaScript, String.padStart() method is used to pad at the starting of the string such that the resulting string is of specified length.


str.padStart(n, pad)
  • str is a string.
  • n is an integer.
  • pad is a string.

padStart() method makes a copy of the string str, pads given pad string to the start of calling string str such that the length of resulting string is n, and returns the resulting string. original string str is not modified. If n is less than length of original str, then padStart() returns original string str as is.


1. In the following program, we pad the string name at start with '#' to make the resulting string of length 10.

let name = 'apple';
let output = name.padStart(10, '#');

2. In the following program, we try to pad the string name to a specific length of n, where n is less than the length of original string.

let name = 'apple';
let output = name.padStart(4, '#');

Copyright @2022