How to pad a string on the left in TypeScript

3 Answers

0 votes
const str = "TS";
 
const padded: string = str.padStart(5, "*");
 
console.log(padded);  
 
 
 
/*
run:
 
***JS
 
*/

 



answered Jul 4, 2025 by avibootz
0 votes
console.log("TS".padStart(9));           
console.log("TS".padStart(9, "*"));     
console.log("TS".padStart(9, "123456"));  
 
 
 
/*
run:
 
"       TS" 
"*******TS" 
"1234561TS" 
 
*/

 



answered Jul 4, 2025 by avibootz
0 votes
const num = 17;
 
const paddedNum: string = String(num).padStart(5, "0"); 
 
console.log(paddedNum);
 
 
 
/*
run:
 
"00017" 
 
*/

 



answered Jul 4, 2025 by avibootz
...