How to use the padEnd() method. In Javascript

The padEnd() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the end (right) of the current string. Similar to padStart(), it takes two parameters: the target length and the pad string.

Example: This example demonstrates how to pad a string using the padEnd() method.

JavaScript
function pad() {
    // Input string
    let example1 = "abcdefg";
    let example2 = 1234;
     
    // Display input string
    console.log(example1);
    console.log(example2);
 
    // Padded string
    let appended_out = String(example1).padEnd(10, '*');
    let appended_out2 = String(example2).padEnd(10, '^#');
     
    // Display output
    console.log(appended_out);
    console.log(appended_out2);
}
 
// Function Call
pad();

Output
abcdefg
1234
abcdefg***
1234^#^#^#




How to pad a string to get the determined length using JavaScript ?

In this article, we will pad a string to get the determined length using JavaScript. To achieve this we have a few methods in javascript which are described below:

Similar Reads

Methods to pad a string to get the determined length:

Table of Content Method 1: Using the padStart() method.Method 2: Custom function using repeat() and slice() method.Method 3: Using JavaScript loopsMethod 4: Using the padEnd() method....

Method 1: Using the padStart() method.

The padStart() method can be used to pad a string with the specified characters to the specified length. It takes two parameters, the target length, and the string to be replaced with. The target length is the length of the resulting string after the current string has been padded. The second parameter is the characters that the string would be padded. If a number is to be padded, it has to be first converted into a string by passing it to the String constructor. Then the padStart() method is used on this string....

Method 2: Custom function using repeat() and slice() method.

The string.repeat() is an inbuilt method in JavaScript that is used to build a new string containing a specified number of copies of the string on which this method has been called....

Method 3: Using JavaScript loops

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement....

Method 4: Using the padEnd() method.

The padEnd() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the end (right) of the current string. Similar to padStart(), it takes two parameters: the target length and the pad string....