Open In App

TypeScript String padStart() method

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The padStart() function, in TypeScript, serves as the opposite of padEnd() and allows you the add characters at the start of a string which is particularly used to format text or for aligning numbers.

Syntax:

string.padStart(targetLength: number, padString?: string): string

Parameters:

  • targetLength: Here you have to pass the length you want the final string to be, after adding padding.
  • padString (Optional): The characters used for padding. Defaults, to spaces (‘ ‘) if not specified.

Return Value:

Returns a new string of the specified targetLength with the padString applied at the left side(i.e. beginning) of the current string.

Example 1: To demonstrate the use of padding with spaces (Default).

Javascript




let originalString: string = "Hello";
let paddedString: string = originalString
    .padStart(10);
console.log(paddedString);


Output:

Hello

Example 2: To demonstrate the use of padding with Zeros.

Javascript




let numberString: string = "123";
let paddedNumber: string = numberString
    .padStart(5, "0");
console.log(paddedNumber);


Output:

00123

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads