##Using .padEnd()
instead of .repeat()
(ES8)
Using .padEnd()
instead of .repeat()
(ES8)
Under certain circumstances, using .padEnd()
instead of .repeat()
saves bytes.
We can take advantage of the following properties:
- the default padding string is a single space
- when provided, the second parameter is implicitly coerced to a string
###Repeating spaces
Repeating spaces
With .repeat()
:
' '.repeat(10)
Using .padEnd()
saves 1 byte:
''.padEnd(10)
###Repeating a dynamic value that needs to be coerced to a string
Repeating a dynamic value that needs to be coerced to a string
With .repeat()
:
x=1;
(x+'').repeat(10)
Using .padEnd()
saves 2 bytes:
x=1;
''.padEnd(10,x)