Skip to content

Instantly share code, notes, and snippets.

@santaklouse
Created April 13, 2026 11:38
Show Gist options
  • Select an option

  • Save santaklouse/54191010bdc4f15742448722a7f9afba to your computer and use it in GitHub Desktop.

Select an option

Save santaklouse/54191010bdc4f15742448722a7f9afba to your computer and use it in GitHub Desktop.
const checkLuhn = (value) =>
String(value)
.replace(/\D/g, '')
.split('')
.reverse()
.map(Number)
.reduce((sum, digit, i) => {
if (i % 2 === 1) {
const doubled = digit * 2;
return sum + (doubled > 9 ? doubled - 9 : doubled);
}
return sum + digit;
}, 0) % 10 === 0;
console.log(checkLuhn("79927398713")); // true
console.log(checkLuhn("4111-1111-1111-1111")); // true (Visa Test)
console.log(checkLuhn(123456789));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment