Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created June 12, 2019 12:24
Show Gist options
  • Save imparvez/b0a5e8b6816850d0d62361e90cfb55b4 to your computer and use it in GitHub Desktop.
Save imparvez/b0a5e8b6816850d0d62361e90cfb55b4 to your computer and use it in GitHub Desktop.
Masking Credit Card Number using JavaScript
function maskCC(number) {
// If given parameter doesn't fall under below category, then return
// 1. Should be numbers(Every element should be a number)
// 2. Should be of length 16
if(!(typeof number === 'string' && number.length === 16 && number.split('').map(Number).every(n => !isNaN(n)) )) {
return;
}
return [...new Array(3).fill('****'), number.slice(-4)].join('-')
}
console.log(maskCC('1234567887654321')) // ****-****-****-4321
console.log(maskCC('aaaaaaaaaaaaaaaa')) // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment