#javascript #javascriptdeveloper Masking Sensitive Data in JavaScript When displaying sensitive values (like credit card numbers), it’s common to hide all but the last few digits for better privacy. JavaScript’s padStart() makes this simple slice out the last four digits, then pad the start with a masking character (* in this case) until it matches the original length. Another padStart() example: Formatting invoice numbers with leading zeros: const invoiceNumber = '42'; console.log(invoiceNumber.padStart(6, '0')); // 000042 Note: The goal here is to demonstrate possible uses of padStart(). For true security, masking should be done on the backend before data is sent to the client. Frontend masking is mainly for UI/UX purposes for example, hiding input while typing, showing only the last few digits for user confirmation, or masking locally generated data.
Great post! padStart is definitely a clean way to do it. Another neat trick for masking is using regex with replace: str.replace(/.(?=.{4})/g, '*') It masks everything except the last 4 characters without even checking the length.
There is a better way than to make 3 more variables
Nice practical tip padStart() is simple and clean for UI masking, while real protection should always happen on the backend.