This content originally appeared on Scotch.io RSS Feed and was authored by James Quick
A few days ago, I was working on building a countdown timer in JavaScript, and I needed to format my seconds and milliseconds. I wanted the seconds to always be a length of 2 digits and the milliseconds to always be a length of 3 digits. In other words I wanted 1
second to display as 01
and 1
millisecond to display as 001
.
I ended up writing my own function to "pad" these numbers, but I found out there are built in functions in JavaScript, padStart() and padEnd() to do just that. In this article, let's look at how to leverage these built-in functions in JavaScript!
Use Cases
Let's start by covering a few different use cases for padding.
Labels and Values
Let's say you have la
This content originally appeared on Scotch.io RSS Feed and was authored by James Quick
James Quick | Sciencx (2020-05-05T17:32:17+00:00) Formatting Strings with JavaScript padStart() and padEnd(). Retrieved from https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.