JavaScript String padEnd() Method



The JavaScript String padEnd() method pads (or extends) the current string with the specified padString to reach the given length. The padding is added at the end of the current string. It can be repeated multiple times if necessary, and if we do not specify this optional parameter padString to this method, it adds the spaces as padding at the end of this string to reach the specified length.

When you pad a string, you add characters to the end of the string until it reaches a certain length.

Syntax

Following is the syntax of JavaScript String padEnd() method −

padEnd(targetLength, padString)

Parameters

This method accepts two parameters named "targetLength" and "padString", which are described below −

  • targetLength − The length of the new string.
  • padString − The string to pad(extend) the current string with.

Return value

This method returns a new string of the specified targetLength, with the padString appended to the end.

Example 1

If the padString parameter is omitted, it will extend the string by adding spaces as padding at the end of the current string until it reaches the specified targetLength.

In the following example, we are using the JavaScript String padEnd() method to pad(extend) a string with spaces as padding at the end of this string "TutorialsPoint" until it reaches the specified targetLength 30.

<html>
<head>
<title>JavaScript String padEnd() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   document.write("Original string: ", str);
   new_str = "";
   document.write("<br>New string length before added padding: ",new_str.length);
   const targetLength = 30;
   document.write("<br>Target Length: ", targetLength);
   //using padEnd() method
   new_str = str.padEnd(targetLength);
   document.write("<br>New string after padding added to the end of this string: ", new_str);
   document.write("<br>New string length after added padding: ",new_str.length);
</script>
</body>
</html>

Output

The above program returns a new string padding with spaces at the end of the string "TutorialsPoint".

Original string: TutorialsPoint
New string length before added padding: 0
Target Length: 30
New string after padding added to the end of this string: TutorialsPoint
New string length after added padding: 30

Example 2

If both padString and targetLength parameters are passed to this method, it extends the string by adding padString at the end until it reaches a certain length.

The following is another example of the JavaScript String padEnd() method. We use this method to extend this string "Hello World" by adding the specified padString "#" at the end of the string until it reaches the specified targetLength 15.

<html>
<head>
<title>JavaScript String padEnd() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   document.write("Original string: ", str);
   new_str = "";
   document.write("<br>New string length before added padding: ",new_str.length);
   const targetLength = 15;
   const padString = "#";
   document.write("<br>Target Length: ", targetLength);
   document.write("<br>Pad string: ", padString);
   //using padEnd() method
   new_str = str.padEnd(targetLength, padString);
   document.write("<br>New string after padding added to the end of this string: ", new_str);
   document.write("<br>New string length after added padding: ",new_str.length);
</script>
</body>
</html>

Output

After executing the above program, it will return a new string with added padding at the end.

Original string: Hello World
New string length before added padding: 0
Target Length: 15
Pad string: #
New string after padding added to the end of this string: Hello World####
New string length after added padding: 15
Advertisements