The last digit of a retail barcode isn't part of the product number. It's a check digit, calculated from all the other digits, so a scanner can catch a misread or a typo.

If you generate barcodes, validate product codes in a form, or import a product catalog, you'll eventually need to calculate or verify one. The good news: EAN-13, UPC-A, EAN-8 and ISBN-13 all use the same algorithm, and it fits in a few lines.

The rule

Take every digit except the check digit. Starting from the rightmost of those digits and moving left, multiply them alternately by 3 and 1. Add everything up. The check digit is whatever you need to add to reach the next multiple of 10.

check = (10 - (sum % 10)) % 10 

The final % 10 handles the case where the sum is already a multiple of 10, so the check digit is 0 instead of 10.

Worked example: EAN-13

Barcode 4006381333931. The first 12 digits are the data, the last 1 is the check digit.

digits: 4 0 0 6 3 8 1 3 3 3 9 3 weights: 1 3 1 3 1 3 1 3 1 3 1 3 products: 4 0 0 18 3 24 1 9 3 9 9 9 → sum = 89 

89 % 10 = 9, and 10 - 9 = 1. The check digit is 1, which matches.

Worked example: UPC-A

UPC-A has 11 data digits. Counting from the right still works, so you don't need a separate formula.

Barcode 036000291452, data 03600029145:

digits: 0 3 6 0 0 0 2 9 1 4 5 weights: 3 1 3 1 3 1 3 1 3 1 3 products: 0 3 18 0 0 0 6 9 3 4 15 → sum = 58 

58 % 10 = 8, 10 - 8 = 2. Check digit 2.

That's why it's easiest to always weight from the right: the rightmost data digit always gets 3, whatever the length.

The function

// Works for EAN-13, EAN-8, UPC-A, ISBN-13 and other GTINs. // Pass the digits WITHOUT the check digit, as a string. function gtinCheckDigit(data) { if (!/^\d+$/.test(data)) throw new Error('Digits only'); let sum = 0; for (let i = 0; i < data.length; i++) { const digit = Number(data[data.length - 1 - i]); sum += digit * (i % 2 === 0 ? 3 : 1); } return (10 - (sum % 10)) % 10; } function isValidGtin(code) { if (!/^\d{8}$|^\d{12,14}$/.test(code)) return false; return gtinCheckDigit(code.slice(0, -1)) === Number(code.at(-1)); } gtinCheckDigit('400638133393'); // 1 isValidGtin('036000291452'); // true isValidGtin('036000291453'); // false 

Bugs I see all the time

Storing the code as a number. UPC codes often start with 0. Put 036000291452 in a numeric column or Number() it, and you get 36000291452, which now fails validation. Barcodes are identifiers, not quantities. Keep them as strings.

Excel eats them. Paste a 13-digit code into a spreadsheet and it may turn into 4.00638E+12. Format the column as text before importing, or you'll lose digits.

Using this for ISBN-10. ISBN-13 (starting with 978 or 979) uses the algorithm above. The old ISBN-10 uses a different mod-11 scheme where the check "digit" can be X. Don't mix them up.

Trusting the check digit for too much. It catches every single-digit error and most swaps of two adjacent digits, but not all of them. It confirms the number was typed or scanned correctly. It doesn't tell you the product exists or that the number is registered to anyone.

Try it

If you just need the answer, QRizeo's barcode check digit calculator does this for EAN and UPC codes, and the EAN-13 barcode generator draws the barcode once the number is right.

Have you run into barcode data that looked fine but failed validation? I'm curious what the cause turned out to be.