{"record":{"id":"08bf04b9af569509","repo":"TheAlgorithms/JavaScript","slug":"the-given-value-is-not-a-string-08bf04","errorCode":null,"errorMessage":"The given value is not a string","messagePattern":"The given value is not a string","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/ValidateCreditCard.js","lineNumber":38,"sourceCode":"      // Multiply every 2nd digit from the left by 2\n      currentDigit *= 2\n      // if product is greater than 10 add the individual digits of the product to get a single digit\n      if (currentDigit > 9) {\n        currentDigit %= 10\n        currentDigit += 1\n      }\n    }\n    validationSum += currentDigit\n  })\n\n  return validationSum % 10 === 0\n}\n\nconst validateCreditCard = (creditCardString) => {\n  const validStartSubString = ['4', '5', '6', '37', '34', '35'] // Valid credit card numbers start with these numbers\n\n  if (typeof creditCardString !== 'string') {\n    throw new TypeError('The given value is not a string')\n  }\n\n  const errorMessage = `${creditCardString} is an invalid credit card number because `\n  if (isNaN(creditCardString)) {\n    throw new TypeError(errorMessage + 'it has nonnumerical characters.')\n  }\n  const creditCardStringLength = creditCardString.length\n  if (!(creditCardStringLength >= 13 && creditCardStringLength <= 16)) {\n    throw new Error(errorMessage + 'of its length.')\n  }\n  if (\n    !validStartSubString.some((subString) =>\n      creditCardString.startsWith(subString)\n    )\n  ) {\n    throw new Error(errorMessage + 'of its first two digits.')\n  }\n  if (!luhnValidation(creditCardString)) {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ValidateCreditCard.js#L20-L56","documentation":"Thrown by validateCreditCard() in String/ValidateCreditCard.js when typeof creditCardString !== 'string'. A TypeError raised before any digit/length/Luhn checks, because the function then reads .length and .startsWith on the value. The card number is expected as a string to preserve leading zeros and to allow prefix checks.","triggerScenarios":"Calling validateCreditCard(4532015112830366) (a Number, which loses precision beyond 2^53 and drops leading zeros), validateCreditCard(null), validateCreditCard(undefined), validateCreditCard([4,5,3,2,...]), validateCreditCard({ number: '...' }).","commonSituations":"Submitting card data as a JSON number instead of a string (very common API mistake); reading input from a numeric-typed form field; passing a BigInt; forgetting to read .value off a DOM input element.","solutions":["Pass the card number as a string: validateCreditCard('4532015112830366').","Serialize card numbers as strings at the API boundary (OpenAPI/JSON schema: type: string).","If you only have a number, coerce carefully — but prefer fixing the producer; numbers above 2^53 already lost precision.","Read .value from form inputs: validateCreditCard(inputEl.value)."],"exampleFix":"// before\nvalidateCreditCard(req.body.cardNumber) // JSON parsed as Number\n\n// after\nvalidateCreditCard(typeof req.body.cardNumber === 'string' ? req.body.cardNumber : String(req.body.cardNumber))\n// and fix the producer: send \"4532015112830366\" (quoted) in JSON","handlingStrategy":"type-guard","validationCode":"function validateCardSafe(v) {\n  if (typeof v !== 'string') throw new TypeError('card number must be a string')\n  return validateCreditCard(v)\n}","typeGuard":"const isCardString = (v) => typeof v === 'string' && /^\\d{13,16}$/.test(v)","tryCatchPattern":"try { validateCreditCard(card) } catch (e) { if (e instanceof TypeError && /not a string/.test(e.message)) card = String(card); else throw e }","preventionTips":["Send card numbers as JSON strings (quoted) to preserve precision and leading zeros.","Schema-validate payloads: cardNumber must be type string at the boundary.","Never let a card number cross an API as a JSON number."],"tags":["string","validation","credit-card","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}