UPC vs EAN: what the digits mean and how the check digit works
If you have ever typed a barcode number by hand and the scanner rejected it, the last digit is usually the reason. It is a check digit, and it exists so that a mistyped or misread number gets caught before anyone trusts
If you have ever typed a barcode number by hand and the scanner rejected it, the last digit is usually the reason. It is a check digit, and it exists so that a mistyped or misread number gets caught before anyone trusts it. This post covers how UPC and EAN numbers relate, what the parts of a number mean, and the few lines of JavaScript that calculate the check digit.
UPC and EAN are the same family
A UPC-A has 12 digits and is the version used in the United States and Canada. An EAN-13 has 13 digits and is used almost everywhere else. They are compatible: a UPC-A is an EAN-13 with a 0 in front. So 036000291452 as a UPC-A is 0036000291452 as an EAN-13, and the check digit is the same 2 in both.
Both are GTINs (Global Trade Item Numbers), the general name for these product identifiers. If you want the longer comparison, the UPC vs EAN guide goes through where each one is used, and what a GTIN is covers the umbrella term.
What the digits mean
A UPC-A splits into a number system digit, a manufacturer code, a product code and the check digit. The UPC code meaning page has the breakdown for each part.
An EAN-13 starts with a GS1 prefix. Take Nutella's EAN-13, 3017620422003. It begins with 301, which belongs to the GS1 office for France and Monaco. That prefix tells you which GS1 office issued the number. It does not tell you where the product was made, which is a common misreading. There is a full list on the barcode country codes page.
Books use the prefixes 978 and 979, so an ISBN-13 is an EAN-13 in disguise. The ISBN-13 9780140328721 is a good example. ISBN-10 is older and uses a different check scheme (mod 11, not the weights below), which is covered in book barcodes and ISBNs.
How the check digit is calculated
The rule is the same for UPC-A, EAN-13 and ISBN-13:
- Take the digits without the check digit.
- Starting from the rightmost one, multiply digits alternately by 3 and 1.
- Add the results.
- The check digit is whatever you need to add to reach the next multiple of 10 (and 0 if the sum is already one).
Worked example with the UPC 03600029145. From the right, the digits and weights give 5×3 + 4×1 + 1×3 + 9×1 + 2×3 + 0 + 0 + 0 + 6×3 + 3×1 + 0×3, which is 58. The next multiple of 10 is 60, so the check digit is 2 and the full UPC is 036000291452.
The same thing in JavaScript
function checkDigit(digits) {
// digits: a string WITHOUT the check digit
let sum = 0;
for (let i = 0; i < digits.length; i++) {
const d = Number(digits[digits.length - 1 - i]);
sum += i % 2 === 0 ? d * 3 : d;
}
return (10 - (sum % 10)) % 10;
}
checkDigit("03600029145"); // 2 -> UPC 036000291452
checkDigit("301762042200"); // 3 -> EAN-13 3017620422003
checkDigit("978014032872"); // 1 -> ISBN-13 9780140328721
Because the weights are applied from the right, the same function works whether the number is 12 or 13 digits long. To validate a full number, strip the last digit, run the function and compare.
What the check digit catches
Every single-digit error is caught, since changing one digit always changes the sum by an amount that is not a multiple of 10. Swapping two neighbouring digits is also caught most of the time. The exception is two adjacent digits that differ by exactly 5, such as 2 and 7, where the swap leaves the sum unchanged. A check digit is a typo detector, not a guarantee the number belongs to a real product.
Checking a number without writing code
If you only need an answer, Online Barcode Scanner is a free browser tool that does this for you. Type or paste a number and it reports whether the check digit is valid, and for EAN-13 it shows the GS1 prefix too. The UPC scanner and ISBN scanner read codes from your camera or an image, and the UPC lookup and EAN lookup pages look the number up in public product databases. Those databases are strongest for groceries, cosmetics and books, and thinner for electronics and hardware.
Try 3017620422003, 049000028904 or 9780140328721 if you want known-good numbers to test against.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.