Open In App

TypeScript String padStart() method

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:

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).




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.




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

Output:

00123
Article Tags :