How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS

Today, we’re going to look at two JavaScript methods you can use to add characters to the beginning and end of a string when it’s less than a certain length.
Let’s dig in!
String.padStart() You can use the String.padStart() method to add characters to the beginning of a string if it’s less than a certain length. This is particularly useful for numbers that need leading 0’s (but can do so much more than that).


This content originally appeared on Go Make Things and was authored by Go Make Things

Today, we’re going to look at two JavaScript methods you can use to add characters to the beginning and end of a string when it’s less than a certain length.

Let’s dig in!

String.padStart()

You can use the String.padStart() method to add characters to the beginning of a string if it’s less than a certain length. This is particularly useful for numbers that need leading 0’s (but can do so much more than that).

The String.padStart() method accepts two arguments: the length the string should be, and what characters to add if it’s not that length. The characters to use is option, and defaults to a space ().

// Add a leading zero for hours below 10
let hour3 = '3';
let hour12 = '12';

// returns "03"
hour3.padStart(2, '0');

// returns "12"
hour12.padStart(2, '0');

Here’s a demo.

String.padEnd()

You can use the String.padEnd() method to add characters to the end of a string if it’s less than a certain length. This is particularly useful for numbers that need trailing 0’s (but can do so much more than that).

The String.padEnd() method accepts two arguments: the length the string should be, and what characters to add if it’s not that length. The characters to use is option, and defaults to a space ().

// Add a leading zero for hours below 10
let minutes0 = '0';
let minutes12 = '12';

// returns "00"
minutes0.padEnd(2, '0');

// returns "12"
minutes12.padEnd(2, '0');

Here’s another demo.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-06-11T14:30:00+00:00) How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS. Retrieved from https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/

MLA
" » How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS." Go Make Things | Sciencx - Friday June 11, 2021, https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Friday June 11, 2021 » How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS., viewed ,<https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/
CHICAGO
" » How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/
IEEE
" » How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/. [Accessed: ]
rf:citation
» How to add characters to the beginning or end of a string if it’s less than a certain length with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/06/11/how-to-add-characters-to-the-beginning-or-end-of-a-string-if-its-less-than-a-certain-length-with-vanilla-js/ |

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.