Categories
TypeScript Answers

How to add left padding to a string with zeros with TypeScript?

Spread the love

Sometimes, we want to add left padding to a string with zeros with TypeScript.

In this article, we’ll look at how to add left padding to a string with zeros with TypeScript.

How to add left padding to a string with zeros with TypeScript?

To add left padding to a string with zeros with TypeScript, we can use the string padStart method.

For instance, we write

const s: string = str.padStart(9, "0");

to prepend the str string with 0’s until the length of it is 9.

Conclusion

To add left padding to a string with zeros with TypeScript, we can use the string padStart method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *