JavaScript String padStart()

Examples

Pad a string with "0" until it reaches the length 4:

let text = "5";
let padded = text.padStart(4,"0");
Try it Yourself »

Pad a string with "x" until it reaches the length 4:

let text = "5";
let padded = text.padStart(4,"x");
Try it Yourself »

Description

The padStart() method pads a string from the start.

The padStart() method pads a string with another string (multiple times) until it reaches a given length.

See Also:

The padEnd() Method

The trim() Method

The trimEnd() Method

The trimStart() Method

Note

The padStart() method is a string method.

To pad a number, convert the number to a string first.

See the example below.

Example

let numb = 5;
let text = numb.toString();
let padded = text.padStart(4,"0");
Try it Yourself »

Syntax

string.padStart(length, string)

Parameters

Parameter Description
lengthRequired.
The length of the resulting string.
stringOptional.
The string to pad with.
Default is space.

Return Value

Type Description
A stringA String of the specified length, with the padding applied from the start.

Browser Support

padStart() is an ECMAScript 2017 feature.

ES2017 is supported in all modern browsers since September 2017:

Chrome 58 Edge 15 Firefox 52 Safari 11 Opera 45
Apr 2017 Apr 2017 Mar 2017 Sep 2017 May 2017

padStart() is not supported in Internet Explorer.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.