Skip to content

JavaScript padStart() Method

  • by

The padStart() method in JavaScript is used to pad a string with a specific character or set of characters at the beginning of the string until the string reaches a certain length.

str.padStart(targetLength, padString)

If padString is too long, it will be truncated from the end to meet targetLength. If padString is not provided, the default padding character is a space (” “).

JavaScript padStart() Method example

Simple example code.

<!DOCTYPE html>
       const str = "hello";

        console.log(str.padStart(10, "x"));

        console.log(str.padStart(5)); // no padding is added



JavaScript padStart() Method

In the first example, the padStart() method adds five spaces at the beginning of the string to make it a total of ten characters long.

Where In the second example, the method uses the “x” character for padding instead of spaces. And the last example no padding is added.

Comment if you have any doubts or suggestions on this JS basic method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading