how to pad string js
const str1 = '5'; console.log(str1.padStart(2, '0')); // expected output: "05" const fullNumber = '2034399002125581'; const last4Digits = fullNumber.slice(-4); const maskedNumber = last4Digits.padStart(fullNumber.length, '*'); console.log(maskedNumber); // expected output: "************5581"
Here is what the above code is Doing:
1. We’re using the slice() method to extract the last 4 digits of the credit card number.
2. We’re using the padStart() method to add the remaining digits back to the string, but replacing them with asterisks (*).
3. We’re using the length property to tell the padStart() method how many total characters the final output string should have.