{"record":{"id":"5fb85814e1ec59bc","repo":"TheAlgorithms/JavaScript","slug":"argument-is-not-a-number","errorCode":null,"errorMessage":"argument is not a Number","messagePattern":"argument is not a Number","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Conversions/RGBToHex.js","lineNumber":3,"sourceCode":"function RGBToHex(r, g, b) {\n  if (typeof r !== 'number' || typeof g !== 'number' || typeof b !== 'number') {\n    throw new TypeError('argument is not a Number')\n  }\n\n  const toHex = (n) => (n || '0').toString(16).padStart(2, '0')\n\n  return `#${toHex(r)}${toHex(g)}${toHex(b)}`\n}\n\nexport { RGBToHex }\n\n// > RGBToHex(255, 255, 255)\n// '#ffffff'\n\n// > RGBToHex(255, 99, 71)\n// '#ff6347'\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/RGBToHex.js#L1-L18","documentation":"Thrown by RGBToHex when any of r, g, or b is not of type 'number'. The guard checks all three with typeof; a single non-number argument (including NaN, which is technically typeof 'number' and will pass) triggers it. It is a TypeError.","triggerScenarios":"Calling RGBToHex('255', 0, 0) (r is a string), RGBToHex(null, 0, 0), or RGBToHex(undefined, 128, 128). Note NaN passes the guard because typeof NaN === 'number' — the function does not range-check.","commonSituations":"Reading color channels from a form/JSON where they arrive as strings; passing a value that was never initialized (undefined); mixing a defaulted optional channel.","solutions":["Coerce channels with Number(...) or parseInt(..., 10) before calling.","Type-check all three in the caller: [r,g,b].every(v => typeof v === 'number' && !Number.isNaN(v)).","Keep a single source of truth for color values as numbers."],"exampleFix":"// before\nRGBToHex('255', 99, 71)\n// after\nRGBToHex(Number('255'), 99, 71)","handlingStrategy":"type-guard","validationCode":"if (![r, g, b].every((v) => typeof v === 'number' && !Number.isNaN(v))) {\n  throw new TypeError('r, g, b must all be finite numbers')\n}\nRGBToHex(r, g, b)","typeGuard":"const areRgbNumbers = (r, g, b) =>\n  [r, g, b].every((v) => typeof v === 'number' && !Number.isNaN(v))","tryCatchPattern":"try {\n  RGBToHex(r, g, b)\n} catch (e) {\n  if (e instanceof TypeError && /not a Number/.test(e.message)) {\n    return RGBToHex(Number(r), Number(g), Number(b))\n  }\n  throw e\n}","preventionTips":["Parse string channels with Number(...) or parseInt(..., 10) at the boundary.","Remember NaN passes typeof === 'number', so also check Number.isNaN.","Keep color values as numbers throughout your pipeline."],"tags":["type-validation","rgb","color","typeof","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}