The Wayback Machine - https://web.archive.org/web/20250102014755/https://www.geeksforgeeks.org/javascript-string-padend-method/
Open In App

JavaScript String padEnd() Method

Last Updated : 16 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The padEnd() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the right end of the string.

Syntax:

string.padEnd( targetLength, padString );

Parameters:

This method accepts two parameters as mentioned above and described below:

  • targetLength: It is the length of the final string once the original string has been padded. If the value is less than the original string length, then the original string is returned.
  • padString: It is the string that is to be padded with the original string. If this value is too long to be within the targetLength, it is truncated.

Return Value:

It returns the final string that is padded with the given string of the given length. 

Example 1: This example uses the padEnd() method to pad strings into another string. 

JavaScript
// Function to implement  padEnd method
function padStrings() {
    // Input methods
    exString = "Hello";
    exString2 = "Geeks";

    // Implement padEnd methods to add symbols at end
    output = exString.padEnd(12, "$");
    output2 = exString2.padEnd(12, "Geeks");

    // Display output
    console.log(output);

    console.log(output2);
}
// Function call
padStrings()

Output
Hello$$$$$$$
GeeksGeeksGe

Example 2: This example uses the padEnd() method to pad numbers into another number. 

JavaScript
// Function to implement padEnd method
function padNumbers() {
    // Input numbers
    exNumber = 1234;
    exNumber2 = 99;

    // Implement padEnd 
    output = String(exNumber).padEnd(8, "0");
    output2 = String(exNumber2).padEnd(8, "**");

    // Display output number
    console.log(output);
    console.log(output2);
}

// Function call 
padNumbers()

Output
12340000
99******

Supported Browsers:

The browser supported by the padEnd() method are listed below:

  • Google Chrome 3
  • Microsoft Edge 12
  • Mozilla Firefox 3.0
  • Safari 5
  • Opera 10.5

JavaScript String padEnd() Method – FAQs

What does the padEnd() method do in JavaScript?

The padEnd() method pads the current string with a specified string (repeated, if needed) so that the resulting string reaches a specified length. Padding is applied from the end of the string.

Can padEnd() truncate the string if it already exceeds targetLength?

No, padEnd() does not truncate the string if it exceeds targetLength. It only pads the string if it is shorter than targetLength.

How does padEnd() differ from padStart()?

padEnd() pads from the end of the string to reach the targetLength, while padStart() pads from the beginning of the string.

Can padEnd() be used to pad strings with characters other than spaces?

Yes, padEnd() can pad strings with any character specified in padString. For example, “hello”.padEnd(10, “-“) would result in “hello—–“.

In what scenarios would you use padEnd()?

padEnd() is commonly used for formatting strings in user interfaces, generating fixed-width output for tables or reports, and ensuring consistent alignment of text in web applications and console outputs.

We have a complete list of Javascript Strings, to check those please go through the Javascript String Complete reference article.


Next Article

Similar Reads

three90RightbarBannerImg