Programming languages can change pretty regularly, and I try to carve out some time periodically to catch up on the latest and greatest the languages I love have to offer.
What really blew my mind is that after I thought about it, I realized I can’t
It’s something that I’ve done quite regularly in PHP by way of
for whatever reason, haven’t had the same need with doing Node.js development.
const str = 'Test'; // Prepend characters console.log(str.padStart(10, ' ')); // Append characters console.log(str.padEnd(10, ' '));
Both methods accept the number of characters you want to pad up to as well as the character (or characters) you’d like to use to do the padding.
This is great for strings, but if you wanted to zero pad a number, you will need to do a bit more work, to convert the number to a string:
const num = 123; // Prepend zeros console.log(number.toString().padStart(6, 0));
And you’re off to the races!