Lodash _.padStart() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.padStart() confidently in real string workflows.

01

Core Syntax

Call _.padStart(string, length, [chars]).

02

Left Padding

Characters prepend until target length.

03

Leading Zeros

Format IDs and order numbers.

04

Tabular Data

Align values in fixed-width fields.

05

padEnd contrast

Left vs right padding for layout.

06

Production Tips

Native padStart() when lodash is not needed.

What Is _.padStart()?

_.padStart() prepends padding characters to the start of a string until it reaches the target length. It is the go-to helper for leading zeros on IDs, order numbers, and fixed-width codes.

💡
Beginner tip

Think of _.padStart('42', 5, '0') as “make this number five digits wide by adding zeros on the left”—result: 00042.

📝 Syntax

javascript
_.padStart(string, [length=0], [chars=' '])

Syntax Rules

  • string — The source string to pad on the left.
  • length — Target total length (defaults to 0).
  • chars — Optional fill string (default space).
  • Return value — New left-padded string.
  • Shorter strings — Returned unchanged when already >= length.
javascript
import padStart from "lodash/padStart";

const id = "42";
const formatted = padStart(id, 5, "0");
// -> "00042"

⚡ Quick Reference

TaskCode patternResult
Leading zeros_.padStart(n, 5, '0')Zero-filled ID
Space pad_.padStart(str, 10)Left-align in field
Invoice #_.padStart(num, 8, '0')INV00000123 style
No change_.padStart('hello', 3)Already long enough
Right pad_.padEnd(str, n)Opposite side
Native altstr.padStart(5, '0')ES2017 built-in
Mutates?
No

Returns new string

Side
Start

Left padding

Common fill
0

Leading zeros

Native
padStart()

Built-in equivalent

🧰 Parameters

string Required

Input string to extend on the left.

_.padStart(id, 6, '0')
length Required

Desired total character count.

_.padStart(s, 10)
chars Optional

Padding character(s); defaults to space.

_.padStart(s, 8, '-')
return value New string

Left-padded copy.

const code = _.padStart(n, 4, '0')

Examples Gallery

Practical _.padStart() patterns with copy-ready code and interactive Try It Yourself labs.

📚 Getting Started

Add leading zeros to a short numeric string.

Example 1 — Leading zeros on a number

Pad 123 to five characters with 0.

javascript
const original = "123";
const padded = _.padStart(original, 5, "0");

console.log(padded);
// -> "00123"
Try It Yourself

How It Works

Zeros prepend until the total length is 5.

📈 Practical Patterns

Space padding and formatted output.

Example 2 — Space-padding a word

Left-pad hello to ten characters with spaces.

javascript
const padded = _.padStart("hello", 10);
console.log(JSON.stringify(padded));
// -> "     hello"
Try It Yourself

How It Works

Default fill is a space when chars is omitted.

Example 3 — Batch numeric padding

Normalize integers to six-digit strings.

javascript
const numbers = [10, 100, 1000, 10000];
const padded = numbers.map(n => _.padStart(String(n), 6, "0"));

console.log(padded);
// -> ["000010", "000100", "001000", "010000"]
Try It Yourself

How It Works

Map over values and pad each string representation.

Example 4 — Custom dash padding

Use hyphens as the fill character.

javascript
const padded = _.padStart("world", 10, "-");
// -> "-----world"

How It Works

Any single character can serve as fill.

Example 5 — Align structured data

Pad object fields for console.table-style output.

javascript
const data = [
  { name: "John", age: 25 },
  { name: "Alice", age: 30 }
];

data.forEach(row => {
  console.log(_.padStart(row.name, 10) + _.padStart(String(row.age), 5));
});

How It Works

Combine padStart on multiple fields for aligned rows.

🚀 Beyond the Basics

Native alternatives.

Example 6 — Native padStart() alternative

ES2017 String.prototype.padStart().

javascript
const str = "123";
const native = str.padStart(5, "0");
const lodash = _.padStart(str, 5, "0");
// both -> "00123"

How It Works

Native padStart() is sufficient when lodash is not imported.

📋 Related string operations

Topic_.padStart_.padEnd_.padpadStart()
Side paddedStart (left)End (right)BothStart (left)
Leading zerosYesNoPossibleYes
MutatesNoNoNoNo
Best forIDs, codesColumn alignCentered textModern native

🧠 How _.padStart() Works

1

Receive string

Input is coerced to a string.

Input
2

Check length

Skip padding if already long enough.

Compare
3

Prepend fill

Repeat fill chars on the left.

Pad
=

Return result

New string; original unchanged.

Done

📝 Notes

  • _.padStart() pads on the left only.
  • Leading zeros for numeric strings is the most common use case.
  • Default fill is a space when chars is omitted.
  • Use _.padEnd() for right-side padding.
  • Strings are immutable—assign the return value.
  • Native String.padStart() covers the same behavior in modern JS.

Conclusion

_.padStart() left-pads strings to a fixed width—ideal for invoice numbers, sortable codes, and aligned tabular output. Pair it with _.padEnd() when you need both sides padded via _.pad().

💡 Best Practices

✅ Do

  • Assign the return value—strings are immutable
  • Specify radix explicitly when parsing user input
  • Use RegExp /g flag for global replacements
  • Validate counts and inputs before transforming
  • Prefer native methods when lodash is not already imported

❌ Don’t

  • Expect the original string variable to change in place
  • Forget radix when using _.parseInt on user data
  • Use string patterns when you need all matches replaced
  • Import all of Lodash for a single string call
  • Skip NaN checks after parsing

Key Takeaways

01

Left pad

Prepend fill chars.

Basics
02

Zeros

Format numeric codes.

Pattern
03

Length

Target total width.

Syntax
04

Tables

Align column values.

Use case
05

padEnd

Right padding sibling.

Related

❓ Frequently Asked Questions

Returns a new string padded on the left to the specified length with fill characters (default space).
No. Strings are immutable.
_.padStart(numStr, width, '0')—e.g. _.padStart('42', 5, '0') -> '00042'.
The string is returned unchanged.
padStart adds on the left; padEnd adds on the right.
String.prototype.padStart() in ES2017.
Did you know?

_.padStart() is how you add leading zeros without manual string concatenation. For right-side padding use _.padEnd().

Practice _.padStart() in the Live Editor

Open Try It, run the examples, and experiment with your own strings.

Open Try It editor →

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.

6 people found this page helpful