JavaScript String padEnd() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

String.prototype.padEnd() pads a string from the end until it reaches a target length (same API as MDN String.prototype.padEnd()). Learn the default space pad, how padString repeats or truncates, when the original string is returned unchanged, how it compares to padStart(), five examples, and try-it labs.

01

Kind

Instance method

02

Returns

New string

03

Pads

Right / end

04

Default pad

Space (" ")

05

Mutates?

No

06

Baseline

Widely available

Introduction

Need a string of a fixed width — for example dotted leaders, fixed-width columns, or short codes padded with spaces? padEnd() adds characters after your text until the result reaches targetLength.

The filler is optional. Omit it and JavaScript uses a single space. Provide a longer filler and it is repeated (and trimmed) to fill the gap. If the string is already long enough, nothing is added.

💡
Beginner tip

padEnd = pad on the right. padStart = pad on the left. Same length rules for both.

This page is part of JavaScript String Methods. Related topics include length and localeCompare().

Understanding the padEnd() Method

str.padEnd(targetLength, padString) builds a new string whose length is at least targetLength, by appending filler after str.

  • Padding is applied at the end of the string.
  • Default padString is " " (one space).
  • If padString is too long for the remaining slots, it is truncated.
  • If targetLength <= str.length, the original contents are returned as-is.
  • An empty padString cannot fill space — the original string is returned.

📝 Syntax

General forms of String.prototype.padEnd:

JavaScript
str.padEnd(targetLength)
str.padEnd(targetLength, padString)

Parameters

  • targetLength — desired length of the result. If less than or equal to str.length, str is returned unchanged.
  • padString (optional) — string used to fill the end. Truncated if longer than needed. Default: " ".

Return value

A string of length targetLength (or the original length when no padding is needed):

SituationResult
Needs paddingstr + repeated/truncated padString
targetLength <= str.lengthOriginal string (no pad)
Omitted padStringSpaces fill the end
Long padStringTruncated to fit exactly
Empty padStringOriginal string (cannot pad)

Common patterns

JavaScript
"abc".padEnd(10);              // "abc       "
"abc".padEnd(10, "foo");       // "abcfoofoof"
"abc".padEnd(6, "123456");     // "abc123"
"abc".padEnd(1);               // "abc"

"Title".padEnd(12, ".");       // "Title......."
"200".padEnd(5);               // "200  "

⚡ Quick Reference

GoalCode
Pad with spacesstr.padEnd(n)
Pad with custom fillerstr.padEnd(n, ".")
Pad on the left insteadstr.padStart(n, "0")
Fixed-width labellabel.padEnd(16) + value
Check current widthstr.length

🔍 At a Glance

Four facts to remember about String.padEnd().

Side
end / right

Filler goes after text

Default
" "

Space character

Too short target
no change

Returns original text

Mutates
no

Strings stay immutable

📋 padEnd() vs padStart() vs manual concat

padEnd()padStart()Manual + / repeat
Where padding goesAfter the textBefore the textWherever you place it
Best forRight-align columns, leadersZero-padded IDs, timesCustom layouts
Truncates fillerYesYesYou must handle it
ReadabilityClear intentClear intentEasy to get wrong

Examples Gallery

Examples follow MDN String.padEnd() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Pad with dots or spaces until a target length.

Example 1 — Basic padEnd()

MDN demo: dotted leaders and default space padding.

JavaScript
const str1 = "Breaded Mushrooms";

str1.padEnd(25, ".");
// "Breaded Mushrooms........"

const str2 = "200";

str2.padEnd(5);
// "200  "
Try It Yourself

How It Works

"Breaded Mushrooms" is 17 characters, so eight "." characters are appended to reach length 25. For "200", two spaces fill to length 5.

Example 2 — Spaces, Repeat, and No-Op

MDN walkthrough of common padEnd results.

JavaScript
"abc".padEnd(10);          // "abc       "
"abc".padEnd(10, "foo");   // "abcfoofoof"
"abc".padEnd(6, "123456"); // "abc123"
"abc".padEnd(1);           // "abc"
Try It Yourself

How It Works

"foo" is repeated until length 10 (abc + foofoof). With targetLength 1, the string is already longer, so nothing is added.

📈 Practical Patterns

Truncation, columns, and edge cases.

Example 3 — Truncating a Long Pad String

Only as much of padString as needed is used — from the start.

JavaScript
"hi".padEnd(10, "*-");
// "hi*-*-*-*-"

"ID".padEnd(8, "0123456789");
// "ID012345"

"ok".padEnd(5, "xyz");
// "okxyz"
Try It Yourself

How It Works

The engine repeats (or slices) the filler so the final .length equals targetLength — never longer.

Example 4 — Fixed-Width Labels

Build simple left-aligned columns for logs or console tables.

JavaScript
const rows = [
  ["Name", "Ada"],
  ["Role", "Engineer"],
  ["City", "London"]
];

const lines = rows.map(([label, value]) =>
  label.padEnd(8, " ") + value
);

lines.join("\n");
// Name    Ada
// Role    Engineer
// City    London
Try It Yourself

How It Works

Each label is padded to width 8 with spaces so values line up in a mono font. For numbers that should stay right-aligned, prefer padStart().

Example 5 — No Pad, Empty Filler, and Length

Three edge cases beginners should know.

JavaScript
"ready".padEnd(5, ".");   // "ready"  (already long enough)
"ready".padEnd(3, ".");   // "ready"

"x".padEnd(8, "");        // "x"      (empty filler cannot pad)

const padded = "Go".padEnd(6, "*");
padded.length;            // 6
padded;                   // "Go****"
Try It Yourself

How It Works

When no padding is possible or needed, you get the original text back. Successful pads always yield result.length === targetLength (measured in UTF-16 code units).

🚀 Common Use Cases

  • Dotted leaderstitle.padEnd(40, ".") for menus or receipts.
  • Fixed-width logs — align labels before values in console output.
  • Table-like text — pad cells so columns line up in monospace fonts.
  • Display formatting — fill short codes or tags to a shared width.
  • Not for visual width — emoji / wide glyphs may look uneven; length is code units.
  • Left padding — use padStart() for leading zeros and right-aligned numbers.

🧠 How padEnd() Builds the Result

1

Read length + filler

Take targetLength and optional padString (default space).

Input
2

Compare lengths

If already long enough (or filler is empty), return the original text.

Guard
3

Fill the end

Repeat and/or truncate padString until the gap is filled.

Pad
4

Return a new string

Original text first, filler last — str itself never changes.

📝 Notes

  • padEnd() pads on the right; padStart() pads on the left.
  • Default filler is one space character.
  • If targetLength <= str.length, the string is returned unchanged.
  • Long fillers are truncated; short fillers are repeated.
  • Empty padString cannot add characters.
  • Length uses UTF-16 code units — same as str.length.

Browser & Runtime Support

String.prototype.padEnd() is Baseline Widely available — supported across modern browsers since April 2017.

Baseline · Widely available

String.padEnd()

Safe for production. Use padEnd for right-side padding; padStart for left-side padding.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
padEnd() Excellent

Bottom line: Use String.padEnd() when you need a fixed width with filler on the right. Remember the default space, truncation rules, and that length is measured in UTF-16 code units.

Conclusion

String.prototype.padEnd() is the clear way to grow a string to a target length by adding characters at the end. Reach for it for leaders, columns, and fixed-width text — and switch to padStart() when the filler belongs on the left.

Continue with the String methods hub or review length and localeCompare().

💡 Best Practices

✅ Do

  • Use padEnd() for right-side / trailing padding
  • Pass a clear padString when spaces are not enough
  • Use monospace fonts when aligning columns in the UI
  • Prefer padStart() for leading zeros on numbers/IDs
  • Check str.length when debugging unexpected widths

❌ Don’t

  • Expect padding when targetLength is too small
  • Pass an empty padString and expect filler
  • Assume length equals visual width for all Unicode
  • Mutate strings — always capture the returned value
  • Hand-roll padding loops when padEnd already fits

Key Takeaways

Knowledge Unlocked

Five things to remember about String.padEnd()

Right-side padding to a target length.

5
Core concepts
🔎 02

Default

space

Rule
✂️ 03

Filler

repeat / truncate

Fill
04

vs padStart

right vs left

Compare
📄 05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.padEnd(targetLength, padString?) returns a new string padded at the end so its length reaches targetLength. The default padString is a single space.
If targetLength is less than or equal to str.length, padEnd() returns the original string unchanged (as a new string with the same contents).
padString is truncated from the end so the result length equals targetLength. For example "abc".padEnd(6, "123456") returns "abc123".
padEnd adds padding after the text (on the right). padStart adds padding before the text (on the left). Both use the same length and padString rules.
No. Strings are immutable. padEnd() returns a new padded string.
padEnd uses UTF-16 code-unit length (same as str.length). Some emoji use two code units, so visual width and .length can differ — pad carefully with Unicode-heavy text.
Did you know?

padStart and padEnd arrived together in ES2017. Before that, developers often wrote helpers with while loops or Array(n + 1).join(pad) — both are easy to get slightly wrong on truncation.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful