Languages
[Edit]
EN

JavaScript - String padEnd() method example

0 points
Created by:
Leen-Kerr
571
// ONLINE-RUNNER:browser;

const text = 'A';

console.log(text.padEnd(1, '*')); // A
console.log(text.padEnd(2, '*')); // A*
console.log(text.padEnd(3, '*')); // A**

1. Documentation

Syntax

String padEnd(maxLength)

String padEnd(maxLength, padString)

Parameters

maxLengthĀ - the lengthĀ ofĀ theĀ resultingĀ stringĀ onceĀ theĀ currentĀ stringĀ hasĀ beenĀ padded.

fillStringĀ -Ā TheĀ stringĀ toĀ padĀ theĀ currentĀ stringĀ with.

ResultA String of the specified maxLength with the fillString applied at the end of the current string.
DescriptionThe method pads the current string with a given string (possibly repeated) so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string.

2. Practical example

In this example, we pad a number to keep only the first 3 digits.

// ONLINE-RUNNER:browser;

const number = '123456789';
const first3Digits = number.slice(6);
const maskedNumber = first3Digits.padEnd(number.length, '*');

console.log(maskedNumber);

Output:

789******

See also

  1. JavaScript - pad right number in string with zeros or spaces to get determined length

References

  1. String.prototype.padEnd() - JavaScript | MDN

Alternative titles

  1. JavaScript - String.prototype.padEnd() documentation with examples
  2. js - String padEnd() method documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (documentation)

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join