{"record":{"id":"63b66b150b37165c","repo":"TheAlgorithms/JavaScript","slug":"argument-is-not-a-string-type","errorCode":null,"errorMessage":"Argument is not a string type","messagePattern":"Argument is not a string type","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Conversions/HexToBinary.js","lineNumber":23,"sourceCode":"    2: '0010',\n    3: '0011',\n    4: '0100',\n    5: '0101',\n    6: '0110',\n    7: '0111',\n    8: '1000',\n    9: '1001',\n    a: '1010',\n    b: '1011',\n    c: '1100',\n    d: '1101',\n    e: '1110',\n    f: '1111'\n  })[key.toLowerCase()] // select the binary number by valid hex key with the help javascript object\n\nconst hexToBinary = (hexString) => {\n  if (typeof hexString !== 'string') {\n    throw new TypeError('Argument is not a string type')\n  }\n\n  if (/[^\\da-f]/gi.test(hexString)) {\n    throw new Error('Argument is not a valid HEX code!')\n  }\n  /*\n    Function for converting Hex to Binary\n\n    1. We convert every hexadecimal bit to 4 binary bits\n    2. Conversion goes by searching in the lookup table\n  */\n\n  return hexString.replace(/[0-9a-f]/gi, (lexeme) => binLookup(lexeme))\n}\n\nexport default hexToBinary\n","sourceCodeStart":5,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/HexToBinary.js#L5-L40","documentation":"Thrown by hexToBinary when hexString is not of type 'string'. This is the first guard in the function and runs before the hex-validity regex. It is a TypeError (not a plain Error) because the contract is about the argument type, not its content.","triggerScenarios":"Calling hexToBinary(255), hexToBinary(null), hexToBinary(['a','b']), or hexToBinary(Buffer.from('ab','hex')) — none of these are strings.","commonSituations":"Passing a Node.js Buffer or Uint8Array thinking it is a string; feeding a parsed JSON number; forgetting to coerce a DOM input value; defaulting the argument to undefined.","solutions":["Coerce to string with String(...) only if the value is genuinely hex text, e.g. String(hexValue).","If you have a Buffer/Uint8Array, convert with buf.toString('hex') first.","Add a typeof hexString === 'string' guard in the caller and surface a clearer error."],"exampleFix":"// before\nhexToBinary(someBuffer)\n// after\nhexToBinary(someBuffer.toString('hex'))","handlingStrategy":"type-guard","validationCode":"if (typeof hexString !== 'string') {\n  throw new TypeError('hexString must be a string')\n}\nhexToBinary(hexString)","typeGuard":"const isString = (x) => typeof x === 'string'","tryCatchPattern":"try {\n  hexToBinary(hexString)\n} catch (e) {\n  if (e instanceof TypeError && /not a string type/.test(e.message)) {\n    return hexToBinary(String(hexString))\n  }\n  throw e\n}","preventionTips":["Convert Buffers/Uint8Arrays with .toString('hex') before calling.","Coerce numeric hex values with String(...) at the boundary.","Default optional parameters to '' rather than undefined."],"tags":["type-validation","string","hex","typeof","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}