For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/string/pad.md.

pad es-toolkit

Pads a string on both sides to reach a specified length.

const padded = pad(str, length, chars);

Usage

pad(str, length, chars?)

Use pad when you want to pad both sides of a string with characters to match a specified length when the string is shorter than the target length. If the padding cannot be evenly distributed on both sides, the right side will have one more character.

import { pad } from 'es-toolkit/string';

// Padding with default whitespace
pad('abc', 8);
// => '  abc   '

// Padding with custom characters
pad('abc', 8, '_-');
// => '_-abc_-_'

// When the string is already longer than or equal to the target length
pad('abc', 3);
// => 'abc'

pad('abcdef', 3);
// => 'abcdef'

When padding characters cannot be evenly distributed to the target length, the right side will be longer.

import { pad } from 'es-toolkit/string';

pad('abc', 9, '123');
// => '123abc123' (left 3 characters, right 3 characters)

pad('abc', 10, '123');
// => '123abc1231' (left 3 characters, right 4 characters)

Parameters

strstringrequired

The string to pad.

lengthnumberrequired

The target length.

charsstringoptional

The characters to use for padding. Defaults to ' '.

Returns

Returns the padded string.

string
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.