Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

EAN-13 and UPC-A use the same modulo-10 check-digit method. Keep the barcode as a string, validate its exact length and ASCII digits, apply alternating weights of 3 and 1 from the rightmost data digit, and calculate (10 - (sum % 10)) % 10. The resulting digit tells you whether the number is arithmetically consistent; it does not prove that GS1 assigned the identifier, identify a product, or certify that a printed symbol will scan.

What EAN-13 and UPC-A actually identify

UPC-A is the 12-digit form of a GTIN-12. EAN-13 is the 13-digit form of a GTIN-13. In both cases, the final character is the check digit and every character before it is data used to calculate that digit.

Format Total digits GTIN type Typical context Digits used as calculation input
UPC-A 12 GTIN-12 Retail numbers for fixed-weight consumer products intended for sale in North America, according to regional GS1 guidance First 11 digits
EAN-13 13 GTIN-13 GTIN-13 retail uses, including markets that use EAN presentation First 12 digits

The checksum algorithm is not different between the formats. The only change relevant to this calculation is the number of data digits: 11 for UPC-A and 12 for EAN-13. A scanner or application may deliver either 12 or 13 characters, so your code should validate the format expected by the receiving field instead of guessing from a product description.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How the modulo-10 check digit is calculated

  1. Separate the data from the supplied check digit. For a complete UPC-A, use the first 11 characters. For a complete EAN-13, use the first 12.
  2. Start at the right edge of the data portion. Give that character a weight of 3.
  3. Move left, alternating weights. The next character gets 1, then 3, then 1, continuing to the left edge.
  4. Multiply each digit by its weight and add the products.
  5. Choose the amount needed to reach the next multiple of ten. In JavaScript, that is (10 - (sum % 10)) % 10.

The second modulo operation matters when the sum is already a multiple of ten: it returns 0 rather than 10. The result is always one decimal digit.

#1 Best Overall
Sale
Tera Barcode Scanner Wireless 1D Laser Cordless Barcode Reader with Battery Level Indicator, Versatile 2 in 1 2.4Ghz Wireless and USB 2.0 Wired
  • Larger battery enables longer continuous usage and twice the stand-by time. With the unique battery indicator light showing the remaining battery level, no more Low Battery Anxiety.
  • The curved handle is extended and widened. With specially designed smooth and flat trigger for a better grip.
  • The orange anti shock silicone protective cover can prevent scratches and friction even when dropped from up to 6.56 feet. IP54 technology protects the wireless barcode scanner from dust.
  • Plug and play with the USB receiver or the USB cable, no driver installation needed. Easy and quick to set up. Wireless transmission distance reaches up to 328 ft. in barrier free environment.
  • Supports almost all 1D Barcodes: Febraban Bank Code, Codabar, Code 11, Code93, MSI, Code 128, EAN-128, Code 39, EAN-8, EAN-13, UPC-A, ISBN, Industrial 25, Interleaved 25, Standard 25, Matrix. Reads damaged, fuzzy, reflective and smudged barcodes.

Worked EAN-13 calculation

Take the 12-digit data portion 400638133393. Weighting starts with its rightmost digit and proceeds left:

Data digit 4 0 0 6 3 8 1 3 3 3 9 3
Weight 1 3 1 3 1 3 1 3 1 3 1 3
Product 4 0 0 18 3 24 1 9 3 9 9 9

The products sum to 89. The next multiple of ten is 90, so the check digit is 1. The complete example is 4006381333931. This demonstrates the arithmetic rule only; the example is not evidence that a particular product is assigned that number.

JavaScript implementation

The safest implementation treats an identifier as text throughout. The code below accepts only ASCII decimal characters, calculates from the right edge, and exposes separate validators for the two fixed-length formats.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function isAsciiDigits(value) {
  return typeof value === 'string' && /^[0-9]+$/.test(value);
}

function calculateGs1CheckDigit(dataDigits) {
  if (!isAsciiDigits(dataDigits)) {
    throw new TypeError('Expected a non-empty string of decimal digits.');
  }

  let sum = 0;
  let weight = 3; // rightmost data digit gets weight 3

  for (let i = dataDigits.length - 1; i >= 0; i--) {
    sum += Number(dataDigits[i]) * weight;
    weight = weight === 3 ? 1 : 3;
  }

  return String((10 - (sum % 10)) % 10);
}

function isValidUpcA(value) {
  return isValidGs1Key(value, 12);
}

function isValidEan13(value) {
  return isValidGs1Key(value, 13);
}

function isValidGs1Key(value, requiredLength) {
  if (
    typeof value !== 'string' ||
    value.length !== requiredLength ||
    !isAsciiDigits(value)
  ) {
    return false;
  }

  const data = value.slice(0, -1);
  const suppliedCheckDigit = value.slice(-1);
  return calculateGs1CheckDigit(data) === suppliedCheckDigit;
}

console.log(calculateGs1CheckDigit('400638133393')); // '1'
console.log(isValidEan13('4006381333931')); // true
console.log(isValidUpcA('036000291452')); // true

Number() is applied to one already-validated character at a time, never to the complete identifier. The returned check digit is a string, so callers do not accidentally turn a leading zero into a missing character.

Rank #2
WoneNice USB Laser Barcode Scanner Wired Handheld Bar Code Scanner Reader Black
  • Plug and play, This laser handheld barcode scanner has simple installation with any USB port and Ideal for businesses, shops and warehouse operations. Its function is unbeatable and easy to use, design is stylish
  • Compatible with Windows, Mac, and Linux; works with Word, Excel, Novell, and all common software
  • Scanning Speed: 200 scans per second. Scanning angle: Inclination angle 55°, Elevation angle 65°. Operational Light Source:Visible Laser 650-670nm.
  • Decode Capability: Code11, Code39, Code93, Code32, Code128, Coda Bar, UPC-A, UPC-E, EAN-8, EAN-13, ISBN/ISSN, JAN.EAN/UPC Add-on2/5 MSI/Plessey, Telepen and China Postal Code,Interleaved 2 of 5, Industrial 2 of 5, Matrix 2 of 5, etc ; 300 configurable options for prefix, suffix and termination strings, support turn on/off the beep.
  • Color: Black. Dimensions: 3.6 x 2.6 x 6.1 inches. Type of Cable: 2M or 6ft straight cable. Shock: 1.5m drop on concrete surface. Regulatory Approvals: FCC CE.

Why the loop starts with weight 3

The loop iterates over the data portion from right to left. Because the first iteration always represents the rightmost data digit, initializing weight to 3 works for both lengths. A left-to-right implementation can also be correct, but it must account for the length-dependent parity; copying a fixed left-side pattern is a common source of EAN/UPC errors.

What the input checks reject

The format validators reject numbers, empty strings, strings containing spaces, line breaks, hyphens, decimal punctuation, and non-ASCII numeral characters. They also reject the wrong length before any arithmetic runs. This strict behavior is appropriate for a validator at an API or database boundary. If a user-facing field should tolerate surrounding whitespace, trim it in a clearly documented input-normalization step and then pass the normalized string to the strict validator; do not silently alter internal characters.

How do I calculate a UPC-A check digit?

Supply the first 11 digits to calculateGs1CheckDigit. For 03600029145, the right-to-left products are weighted 3, 1, 3, 1 and so on. Their sum is 58, and (10 - (58 % 10)) % 10 returns 2. Appending that result gives 036000291452, which the validator accepts as a check-digit-valid UPC-A.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not pass a 12-digit UPC-A to the calculation function when you intend to generate a new digit: the supplied final digit would be treated as data and the result would be wrong. Pass the data portion only. Conversely, validation functions receive the complete fixed-length value and split off the final character themselves.

Rank #3
Eyoyo EYH2 Handheld USB Wired 2D 1D Barcode Scanner for POS Mobile Payment | Automatic QR Code Scanner PDF417 Data Matrix, Long USB Cable, for Convenience Store, Supermarket, Warehouse
  • Continuous Usage All Day: The EY-H2 USB barcode scanner is designed to always be ready for the next scan, which significantly reduces downtime and repair costs; it shortens checkout lines, improves customer service, and boosts business productivity
  • Plug and Play: Eyoyo wired barcode scanner is connected via a USB cable, with no need to install any driver or software; It offers effortless connection and is compatible with Windows, Mac, Android, and Linux; Seamlessly works with Quickbook, Word, Excel, Novell, and all common software
  • Supports Multiple 1D/2D Barcodes: Eyoyo QR code scanner scan with most 1D 2D barcodes with ease; 1D Barcodes: EAN, UPC, Code 39, Code 93, Code 128, UCC/EAN 128, Codabar, Interleaved 2 of 5, ITF-6, ITF-14, ISBN, ISSN, MSI-Plessey, GS1 Databar, Code 11, Industrial 25, Matrix 2 of 5, etc. 2D Barcodes: QR, DataMatrix, PDF417, and so on
  • Supports Screen Scanning: The Eyoyo 2D scanner is capable of reading barcodes from smartphone screens, such as mobile coupons, digital wallets, and digital loyalty cards; Before scanning, simply turn your screen brightness to the maximum
  • Sturdy Anti-Shock and Durable Design: The Eyoyo 2D barcode scanner features an ergonomic design made of high-quality ABS, enabling it to withstand repeated drops from 5 ft/1.5 m high onto the concrete ground; The durable plastic material ensures a long service life

How can I validate an EAN-13 barcode in JavaScript?

Call isValidEan13 with the complete 13-character string. For example:

const scanned = '4006381333931';

if (isValidEan13(scanned)) {
  console.log('Passes the EAN-13 check-digit test');
} else {
  console.log('Invalid EAN-13 syntax or check digit');
}

A result of true means only that the input has 13 ASCII digits and its final digit matches the modulo-10 calculation from the first 12. Replacing the final 1 in this example with another digit makes the arithmetic test fail. The function does not perform a registry lookup or inspect an image of the barcode.

Why identifiers should remain strings

Leading zeroes are meaningful characters. Converting 036000291452 to a number loses its first zero, making a later length check fail or producing a different identifier when the value is serialized. A string also preserves the exact scanned representation for logs, database keys, and API payloads.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

JavaScript numbers have exact integer precision only through Number.MAX_SAFE_INTEGER, whose limit is 2^53 - 1 (with the corresponding negative bound). GTIN-12 and GTIN-13 values are shorter than that numeric bound, but numeric conversion is still the wrong identifier-handling pattern: it discards formatting information and creates avoidable type and serialization problems. The implementation needs numeric conversion only for individual validated characters, each of which is a single decimal digit.

Rank #4
NETUM Bluetooth Barcode Scanner, Support 2.4G Wireless & Bluetooth & Wired Connect Smart Phone, Tablet, PC, CCD Bar Code Reader Work with Windows, Mac,Android (NT-1228BC)
  • Widely Compatible: Bluetooth Barcode Scanner for iPhone iPad Android Tablet PC, Support HID / SPP / BLE mode via bluetooth, Work with Windows XP/7/8/10, Mac OS, Windows Mobile, Android OS, iOS, Linux.
  • Strong Recognition Ability: With the 2500 pixels high-resolution CCD sensor Engine, Rapidly decodes all 1D and stacked barcodes (including ISBN book), even worn, damaged or tightly spaced codes. Scan 1D codes directly from paper or screen, such as a computer monitor, smartphone, or tablet, or scan through glass surfaces, plastic shrink wrap, a CCD scanner is likely the best way to go.
  • Automatic Scanning: NT-1228bc barcode scanner have three scanning modes: manual trigger mode, continuous scanning mode and auto-sensing scanning mode. In addition, there is a storage mode. Storage mode can be used when you are out of range of Bluetooth and wireless connectivity. Supports storage of up to 100,000 barcodes. Note: Before use, you need to scan the corresponding setting barcode on the manual.
  • 2600mAh Battery Upgraded: Continuous scanning up to 200,000 times on a full charge. After a full charge the scanner can be used for one month at least, even in warehouses and at pos checkout counters where scanners are frequently used. In libraries and hospitals it can be used even longer.
  • Programmable Configuration: Add custom prefixes/ suffixes, delete characters, Add keyboard keys/ combinations (terminator TAB, CR&LF, Home etc.), Enable or disable the barcode type as you want. Buzzer can be set to mute to allow for a quiet operation.(Note: It does not work with square POS / Divalto / DoorDash / Lightspeed POS system)

EAN-13 and UPC-A: what to choose in an application

Decision point UPC-A EAN-13
Expected complete length 12 characters 13 characters
GTIN representation GTIN-12 GTIN-13
Data length before check digit 11 characters 12 characters
Checksum Modulo 10 with alternating 3 and 1 weights from the rightmost data digit Exactly the same algorithm
Typical regional/context description North American retail numbers for fixed-weight consumer products, in the cited GS1 regional guidance GTIN-13 retail uses, including EAN presentation
Validator to call isValidUpcA(value) isValidEan13(value)

Choose the validator based on the identifier contract your system expects, not on the country inferred from a prefix. A check digit does not encode a reliable product description or business meaning.

What a passing check digit does—and does not—prove

  • It does prove: the supplied final digit is arithmetically consistent with the preceding digits under the GTIN modulo-10 rule, and that the value passed your format and character checks.
  • It does not prove GS1 allocation: only the relevant GS1 records or business systems can establish whether an identifier was issued to a company. GS1 explains that the complete number has no product meaning that can be decoded without database access.
  • It does not identify a product: use an authorized registry, catalog, or internal product database when your workflow needs ownership, description, pack size, market, or status.
  • It does not certify print quality: arithmetic cannot assess quiet zones, bar and space contrast, symbol dimensions, distortion, damage, or scanner readability. Those require an appropriate barcode-symbol verification process.

Use the label check-digit valid or passes the GTIN check-digit test. Avoid calling a value authentic, official, assigned, or product-verified solely because this function returned true.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Integrating scanner input safely

Many USB and Bluetooth barcode readers behave like keyboards: they type the decoded characters into the focused field and may append an Enter or Tab key. A scanner is useful for exercising that input path, but it is not required to implement or unit-test the arithmetic, and it does not replace symbol-quality verification.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Capture the scanner value as text, not as a numeric form control value.
  2. Define whether a trailing Enter, Tab, or surrounding whitespace is transport framing. Remove only the characters your input contract explicitly permits.
  3. Choose the expected format from the transaction context, then call isValidUpcA or isValidEan13.
  4. On failure, report whether the problem was wrong length/characters or a mismatched check digit, without silently repairing the identifier.
  5. If the business process requires identity or product details, perform a separate database lookup after the arithmetic check.
  6. If scans fail intermittently, investigate symbol quality and reader configuration independently of the JavaScript checksum result.

Troubleshooting common implementation mistakes

The validator rejects values that visibly look correct

Log the exact string and its length. A lost leading zero, an appended carriage return, or a hidden space changes the input. Check that the application is not receiving a formatted display value such as one containing hyphens.

Best Value
NetumScan USB 1D Barcode Scanner, Handheld Wired CCD Barcode Reader (1)
  • CCD Image Scanning Technology - NetumScan 1D barcode reader is equiped with advanced CCD sensor, which can quick capture 1D codes from paper and screen, including CODE128, UPC/EAN Add on 2 or 5, that can read even deformed barcodes, i.e. smudged, damaged, fuzzy, reflective barcodes, etc. Reading faster and more accurate than laser scanner.
  • Sturdy Anti-shock and Durable Design - Ergonomic design with high-quality ABS making it can support withstand repeated drops from 2m high to the concrete ground, durable to use. Durable plastic material guarantees long service life.
  • Three scanning mode - Key trigger mode + Auto-induction mode + Continuous Mode. There is no need to pull the trigger in auto-sensing mode and continuous scanning. Sometimes the self-sensing scanning function is in the inactive stage, please contact us and be at your service at any time.
  • Supported 1D Bar Code - 1D Decode Capability: UPC-A, UPC-E, EAN-8, EAN-13, ISSN, ISBN, Code 128, GS1-128, Code39, Code93,Code32, Code11, UCC/EAN128, Interleaved 2 of 5, Industrial 2 of 5, Codabar(NW-7), MSI, Plessey, RSS, China Post, etc.
  • Widely Use Range - This NetumScan Handheld USB barcode scanner can be used in supermarkets, convenience stores, warehouse, library, bookstore, drugstore, retail shop for file management, inventory tracking and POS(point of sale), etc.

The calculated digit is consistently wrong

Verify that the final supplied digit was removed before calculation, that iteration starts at dataDigits.length - 1, and that the first weight is 3. Starting from the left with a fixed pattern produces a parity error when switching between 11-digit UPC-A data and 12-digit EAN-13 data.

Non-English numerals pass unexpectedly

Use the ASCII-only regular expression shown above when the interchange format requires the characters 0 through 9. Broad Unicode digit matching can admit characters that are not valid GTIN text and may not behave as expected with Number().

A value passes but the product cannot be found

That is an identity or allocation problem, not a checksum problem. Keep the arithmetic result and the database or registry lookup as separate fields and separate decisions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Conformance and version context

GS1 General Specifications are maintained in dated releases. The downloadable specification search result identifies Release 24.0, ratified January 2024, while GS1 Ireland’s specifications page identifies Release 26, ratified January 2026. For production conformance, consult the current release applicable to your operation; the checksum rule described here is the shared fixed-length GTIN method, not a substitute for all packaging, allocation, or symbol requirements.

For a small utility, the functions above are sufficient. In a production service, add tests for both lengths, leading-zero values, non-ASCII input, whitespace policy, wrong-length input, and known valid and invalid check digits. Keep the original scanned string available for audit purposes, and document clearly that the boolean result is an arithmetic validation status rather than proof of product authenticity.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.