Quick Tips: String Padding in JS
nikhilmwarrier
Posted on May 28, 2021
Quick Tip #1: String Padding in JavaScript
Use the padStart()
string method with the string length and the string to fill the padding with.
Example:
const text = "Party"
text.padStart(7, "🎉 ") //"🎉 Party"
Actual Use Case
Suppose you have an array of binary numbers which need not necessarily be in the standard 8-digit format. We want to add "padding" to it so it looks like the 8-digit eyecandy we all know and love.
Here is how to do it:
const binaryNums = [0, 1101, 011010, 1010]
const paddedBinary = []
binaryNums.forEach(num => {
const str = num.toString(); // convert to string
const paddedStr = str.padStart(8, "0") // fix the length of string at 8, and add padding with "0"
paddedBinary.push(Number(paddedStr)
})
// paddedBinary = [00000000, 00001101, 00011010, 00001010]
See the MDN Docs for more info.
💖 💪 🙅 🚩
nikhilmwarrier
Posted on May 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.