Example 1 — Leading zeros on a number
Pad 123 to five characters with 0.
const original = "123";
const padded = _.padStart(original, 5, "0");
console.log(padded);
// -> "00123" How It Works
Zeros prepend until the total length is 5.

By the end of this tutorial, you’ll use Lodash’s _.padStart() confidently in real string workflows.
Call _.padStart(string, length, [chars]).
Characters prepend until target length.
Format IDs and order numbers.
Align values in fixed-width fields.
Left vs right padding for layout.
Native padStart() when lodash is not needed.
_.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.
Think of _.padStart('42', 5, '0') as “make this number five digits wide by adding zeros on the left”—result: 00042.
_.padStart(string, [length=0], [chars=' ']) 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.import padStart from "lodash/padStart";
const id = "42";
const formatted = padStart(id, 5, "0");
// -> "00042" | Task | Code pattern | Result |
|---|---|---|
| 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 alt | str.padStart(5, '0') | ES2017 built-in |
NoReturns new string
StartLeft padding
0Leading zeros
padStart()Built-in equivalent
string RequiredInput string to extend on the left.
_.padStart(id, 6, '0')length RequiredDesired total character count.
_.padStart(s, 10)chars OptionalPadding character(s); defaults to space.
_.padStart(s, 8, '-')return value New stringLeft-padded copy.
const code = _.padStart(n, 4, '0')Practical _.padStart() patterns with copy-ready code and interactive Try It Yourself labs.
Add leading zeros to a short numeric string.
Pad 123 to five characters with 0.
const original = "123";
const padded = _.padStart(original, 5, "0");
console.log(padded);
// -> "00123" Zeros prepend until the total length is 5.
Space padding and formatted output.
Left-pad hello to ten characters with spaces.
const padded = _.padStart("hello", 10);
console.log(JSON.stringify(padded));
// -> " hello" Default fill is a space when chars is omitted.
Normalize integers to six-digit strings.
const numbers = [10, 100, 1000, 10000];
const padded = numbers.map(n => _.padStart(String(n), 6, "0"));
console.log(padded);
// -> ["000010", "000100", "001000", "010000"] Map over values and pad each string representation.
Use hyphens as the fill character.
const padded = _.padStart("world", 10, "-");
// -> "-----world" Any single character can serve as fill.
Pad object fields for console.table-style output.
const data = [
{ name: "John", age: 25 },
{ name: "Alice", age: 30 }
];
data.forEach(row => {
console.log(_.padStart(row.name, 10) + _.padStart(String(row.age), 5));
}); Combine padStart on multiple fields for aligned rows.
Native alternatives.
ES2017 String.prototype.padStart().
const str = "123";
const native = str.padStart(5, "0");
const lodash = _.padStart(str, 5, "0");
// both -> "00123" Native padStart() is sufficient when lodash is not imported.
| Topic | _.padStart | _.padEnd | _.pad | padStart() |
|---|---|---|---|---|
| Side padded | Start (left) | End (right) | Both | Start (left) |
| Leading zeros | Yes | No | Possible | Yes |
| Mutates | No | No | No | No |
| Best for | IDs, codes | Column align | Centered text | Modern native |
_.padStart() WorksInput is coerced to a string.
Skip padding if already long enough.
Repeat fill chars on the left.
New string; original unchanged.
chars is omitted.String.padStart() covers the same behavior in modern JS._.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().
Prepend fill chars.
BasicsFormat numeric codes.
PatternTarget total width.
SyntaxAlign column values.
Use caseRight padding sibling.
Related_.padStart() is how you add leading zeros without manual string concatenation. For right-side padding use _.padEnd().
Open Try It, run the examples, and experiment with your own strings.
6 people found this page helpful