I am in need of a JavaScript function which can take a value and pad it to a given length (I need spaces, but anything would do). I found this:
Code:
The key trick in both those solutions is to create an array
instance with a given size (one more than the desired length), and then to immediately call the join()
method to make a string
. The join()
method is passed the padding string
(spaces probably). Since the array
is empty, the empty cells will be rendered as empty strings
during the process of joining the array
into one result string
, and only the padding will remain. It's a really nice technique.