Formatting Strings with JavaScript padStart() and padEnd()

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 digit…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Formatting Strings with JavaScript padStart() and padEnd()." James Quick | Sciencx - Tuesday May 5, 2020, https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/
HARVARD
James Quick | Sciencx Tuesday May 5, 2020 » Formatting Strings with JavaScript padStart() and padEnd()., viewed ,<https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/>
VANCOUVER
James Quick | Sciencx - » Formatting Strings with JavaScript padStart() and padEnd(). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/
CHICAGO
" » Formatting Strings with JavaScript padStart() and padEnd()." James Quick | Sciencx - Accessed . https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/
IEEE
" » Formatting Strings with JavaScript padStart() and padEnd()." James Quick | Sciencx [Online]. Available: https://www.scien.cx/2020/05/05/formatting-strings-with-javascript-padstart-and-padend/. [Accessed: ]
rf:citation
» Formatting Strings with JavaScript padStart() and padEnd() | James Quick | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.