JavaScript String padEnd()

Total
0
Shares

In this tutorial, we will learn about the JavaScript String padEnd() method with examples.

The JavaScript String.prototype.padEnd() method pads the current string with another string until the resulting string reaches the given length. The padding is applied from the end of the current string.

padEnd() Example

const text = "ItsJavaScript"
console.log(text.padEnd(25, '.'))

Output

ItsJavaScript............

padEnd() Syntax

The syntax of the JavaScript padEnd() method is as follows.

str.padEnd(targetLength, padString)

Here the str is the string or the string variable.

padEnd() Parameter

The JavaScript padEnd() method can take two parameters:

  • targetLength – The length of the final string after the current string has been padded. If the targetLength is less than the length of the string, then the string is returned unmodified without padding.
  • padString (Optional) – The padString is an optional parameter that is used to pad the string. The default value is ” ” 

Note: If padString is too long, it will be truncated from the end to meet the targetLength.

padEnd() Return Value

The padEnd() method returns the string of the specified length with the padString applied at the end of the string.

Example 1 – How padEnd() method works in JavaScript?

// How to pad a string at the end
const text = "Microsoft"
console.log(text.padEnd(20,'.'))

// Pad string in case length is less than the string length
const text2 = "LinkedIn"
console.log(text2.padEnd(5,'corp'))

// pad another string at the end
const text3 = "Apple"
console.log(text3.padEnd(9, ' Inc'))

// the padstring gets truncated to meet the target length
const text4 = "ItsJavaScript"
console.log(text4.padEnd(25, ' is very easy to learn'))

Output

Microsoft...........
LinkedIn
Apple Inc
ItsJavaScript is very eas

Example 2 – Working of padEnd() method with default value

The padString is an optional parameter that is used to pad the string. The default value is ” ” if we do not provide it.

// How to pad a string at the end
const text = "Microsoft"
console.log(text.padEnd(20))

// Pad string in case length is less than the string length
const text2 = "LinkedIn"
console.log(text2.padEnd(5))

Output

"Microsoft           "
"LinkedIn"
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript String padStart()

JavaScript String padStart()

Table of Contents Hide padStart() ExamplepadStart() SyntaxpadStart() ParameterpadStart() Return ValueExample 1 – How padStart() method works in JavaScript?Example 2 – Working of padStart() method with default value In this tutorial,…
View Post
JavaScript String endsWith()

JavaScript String endsWith()

Table of Contents Hide endsWith() SyntaxendsWith() ParametersendsWith() Return ValueJavaScript String endsWith() examplesJavaScript endsWith() example with length parameter In JavaScript, the endsWith() method determines whether the given string ends with a…
View Post