What Is a Check Digit - EAN-13, ISBN, and Luhn

A check digit is a single verification digit appended to the end of barcodes, membership numbers, credit card numbers, and the like. Because it is derived from the preceding digits by a fixed calculation, mistyping even one digit of the number breaks the calculation, so you can catch the error on the spot. This article explains, in concrete terms with hand calculation and code examples, how the check digit is calculated for EAN-13/JAN, ISBN-13, and credit cards (Luhn).

Key point: The idea behind a check digit is to "multiply each digit by a weight, sum the results, and choose the final digit so that the total is a multiple of 10." Only the way the weights are assigned differs between schemes; the underlying principle is the same.

1. The role of a check digit

Whenever a person types a number by hand or a barcode is scanned, a certain fraction of errors will inevitably occur. A check digit is a mechanism that appends a verification digit, computed from the body of the number, to the end, so that the receiving side can redo the same calculation and confirm that the trailing digit matches.

2. How to calculate EAN-13 / JAN

Japan's product barcode (JAN) is the same as the international standard EAN-13: of the 13 digits, the first 12 are the body and the last one is the check digit. The calculation steps are as follows.

  1. Multiply the 12-digit body by the weights 1, 3, 1, 3, … from the left (odd positions get 1, even positions get 3).
  2. Sum all of the multiplied results.
  3. Take the remainder of the sum divided by 10, then subtract that remainder from 10 (if the result is 10, make it 0). This is the check digit.

As an example, let's calculate with the body 400638133393. The sum of the odd positions (4,0,3,1,3,9) is 20, and the sum of the even positions (0,6,8,3,3,3) is 23, which multiplied by 3 gives 69. The total is 20 + 69 = 89. The remainder of 89 divided by 10 is 9, so 10 − 9 = 1 is the check digit. The completed number is therefore 4006381333931.

3. How to calculate ISBN-13

A book's ISBN-13 is a 13-digit number beginning with 978 or 979, and the check digit calculation is exactly the same as EAN-13 (alternating weights of 1 and 3). The old 10-digit ISBN (ISBN-10) uses different weighting, and its check digit can be X (= 10), which is a difference; but the 13-digit numbers in circulation today can be calculated with the EAN-13 rule.

4. How to calculate credit cards (Luhn)

Credit card numbers, IMEIs, and the like are verified with the Luhn algorithm (modulus 10). The steps are as follows.

  1. Leaving the position for the check digit empty, double every other digit starting from the rightmost.
  2. If doubling produces two digits (10 or more), add its individual digits together (for example, 14 → 1 + 4 = 5; this is the same as subtracting 9).
  3. Sum all the digits, and make the check digit the amount needed so the total becomes a multiple of 10 (= 10 − the ones place of the total; if 10, then 0).

For the body 7992739871, the total comes to 67, so 10 − 7 = 3 is the check digit. When you run the completed number 79927398713 through the same procedure, including the trailing digit, the total becomes a multiple of 10 and it is judged valid.

5. How to try it yourself (JavaScript)

You can verify the calculation by running the following code as-is in your browser console or in Node.js.

// Check digit for EAN-13 / ISBN-13
function ean13(body /* 12-digit string */) {
  let sum = 0;
  for (let i = 0; i < 12; i++) sum += (+body[i]) * (i % 2 === 0 ? 1 : 3);
  return (10 - (sum % 10)) % 10;
}
console.log(ean13("400638133393")); // 1

// Luhn check digit (credit cards, etc.)
function luhn(body) {
  let sum = 0, dbl = true;
  for (let i = body.length - 1; i >= 0; i--) {
    let n = +body[i];
    if (dbl) { n *= 2; if (n > 9) n -= 9; }
    sum += n; dbl = !dbl;
  }
  return (10 - (sum % 10)) % 10;
}
console.log(luhn("7992739871")); // 3

The results should match your hand calculation. If you would rather check it in the browser with just a few button clicks, enter your number into the calculator tool below.

Free Tool Verify with the Check Digit Calculator Choose EAN-13/JAN, UPC-A, ISBN-13, or Luhn, enter a number, and it calculates the check digit and verifies the trailing digit. Everything runs entirely in your browser.

Frequently Asked Questions (FAQ)

What is a check digit for?

It is there to detect keying and reading errors in a number. Because the final digit is derived by calculation from the preceding digits, if any digit in the number is off by one the calculation no longer matches, revealing that the number is incorrect. This catches many of the mistakes humans commonly make, such as mistyping a single digit or swapping two adjacent digits.

Why are the EAN-13 weights 1 and 3?

It makes single-digit errors and swaps of two adjacent digits easier to detect. When you alternate weights whose difference is 2 or more, like 1 and 3, swapping adjacent digits tends to change the total, so the error can be detected. It is not perfect, however, because some swaps, such as when the difference is 3, are missed.

Are Luhn and modulus 10 the same thing?

The Luhn algorithm is a type of modulus 10 scheme, and it is the specific procedure widely used for things like credit card numbers. You double every other digit starting from the rightmost, add the digits together when doubling produces a value of 10 or more (which is equivalent to subtracting 9) to form a total, and choose the check digit so that the total is a multiple of 10.

← Back to the tech blog index