Lodash _.pad() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Pads string on the left and right so the result reaches length. Default fill is a space. Part of Lodash String (since 3.0.0 — not deprecated).

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.pad. Start with this line.

Lodash
_.pad([string=''], [length=0], [chars=' '])
CallWhat it does
_.pad([string=''], [length=0], [chars=' '])Primary signature.
_.pad(string)Transform or inspect a string.

Prefer import pad from "lodash/pad" when you want a smaller bundle.

Library declarations
Lodash
_.pad([string=''], [length=0], [chars=' '])

Parameters

ParameterDescription
stringOptional. Value to pad (default '')
lengthOptional. Target length (default 0)
charsOptional. Fill string (default ' ')

Return Value

Return ValueDescription
*New padded string

Lodash Examples

Example 1 — Official: Space Padding

Center abc inside width 8 with spaces.

Lodash
console.log(JSON.stringify(_.pad("abc", 8)));
// "  abc   "
Try It Yourself

How It Works

  1. Need. 5 pad characters for width 8.
  2. Split. 2 on the left, 3 on the right.
  3. Fill. Default chars is a space.
  4. Docs. Matches the official first example.

Example 2 — Official: Custom Fill

Repeat _- on both sides to reach length 8.

Lodash
console.log(_.pad("abc", 8, "_-"));
// '_-abc_-_'
Try It Yourself

How It Works

  1. Need. Still 5 pad characters.
  2. Left. First 2 chars of the fill pattern → _-.
  3. Right. Next 3 chars → _-_.
  4. Note. Lodash 4.x yields '_-abc_-_' (length 8).

Example 3 — Official: Already Long Enough

No padding when the string already meets the length.

Lodash
console.log(_.pad("abc", 3));
// 'abc'

console.log(_.pad("abc", 2));
// 'abc'  — not truncated
Try It Yourself

How It Works

  1. Check. Compare string.length to length.
  2. Equal. Length 3 → return as-is.
  3. Shorter target. Still returns the full string.
  4. Truncate? Use _.truncate if you need cutting.

Example 4 — pad vs padStart / padEnd

Same width and fill — different sides.

Lodash
var s = "abc";

console.log(_.pad(s, 8, "_-"));      // '_-abc_-_'
console.log(_.padStart(s, 8, "_-")); // '_-_-_abc'
console.log(_.padEnd(s, 8, "_-"));   // 'abc_-_-_'
Try It Yourself

How It Works

  1. pad. Splits fill across both sides.
  2. padStart. All fill goes to the left.
  3. padEnd. All fill goes to the right.
  4. Pick. Center columns vs leading/trailing align.

Example 5 — Odd Leftover Goes Right

When the pad width is odd, the extra character lands on the right.

Lodash
console.log(_.pad("abc", 9, "_-"));
// '_-_abc_-_'
// left 3 + abc + right 3
Try It Yourself

How It Works

  1. Need. 9 − 3 = 6 pad characters.
  2. Even. 3 left and 3 right.
  3. Pattern. Fill repeats as _-_ on each side.
  4. Rule. For odd needs, right side gets the extra char.

Browser & Runtime Support

_.pad is a Lodash String helper available since 3.0.0. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native padStart/padEnd (ES2017) cover one side each; Lodash _.pad remains the concise both-sides helper.

✓ Stable · Lodash 4.x

_.pad()

Available in [email protected] full builds and the lodash/pad modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
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
_.pad Since 3.0.0

Bottom line: Safe for production once Lodash is on the page. Prefer import pad from "lodash/pad" for smaller builds. Ideal for centered fixed-width labels and console columns.

Frequently Asked Questions

_.pad([string=''], [length=0], [chars=' ']) pads string on both sides until it reaches length. Spaces are the default fill.
_.pad centers by padding left and right. _.padStart pads only the left; _.padEnd pads only the right.
If string.length >= length, the original string is returned unchanged. Official: _.pad('abc', 3) → 'abc'.
Yes. Pass chars like '_-'. Lodash repeats/truncates the fill to fit the needed pad width on each side.
There is no single native center-pad. You can combine String#padStart and String#padEnd, or keep using _.pad.
Use import pad from "lodash/pad" or require("lodash/pad"). Full build: import _ from "lodash" then _.pad.

Next: _.padEnd()

Pad a string on the right to a target length.

_.padEnd() tutorial →

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