{"record":{"id":"a0abd5b1d38277c8","repo":"TheAlgorithms/JavaScript","slug":"argument-is-nan-not-a-number","errorCode":null,"errorMessage":"Argument is NaN - Not a Number","messagePattern":"Argument is NaN - Not a Number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/Abs.js","lineNumber":16,"sourceCode":"/**\n * @function abs\n * @description This script will find the absolute value of a number.\n * @param {number} num - The input integer\n * @return {number} - Absolute number of num.\n * @see https://en.wikipedia.org/wiki/Absolute_value\n * @example abs(-10) = 10\n * @example abs(50) = 50\n * @example abs(0) = 0\n */\n\nconst abs = (num) => {\n  const validNumber = +num // converted to number, also can use - Number(num)\n\n  if (Number.isNaN(validNumber) || typeof num === 'object') {\n    throw new TypeError('Argument is NaN - Not a Number')\n  }\n\n  return validNumber < 0 ? -validNumber : validNumber // if number is less than zero means negative, then it converted to positive. i.e., n = -2 = -(-2) = 2\n}\n\nexport { abs }\n","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Abs.js#L1-L23","documentation":"Thrown by abs(num) as a TypeError when the coerced value Number(num) is NaN OR when typeof num === 'object'. The function coerces with the unary +, so non-numeric strings and undefined become NaN; objects (including null, arrays, and {}) are rejected outright even if they coerce to a number (e.g. [] -> 0, null -> 0).","triggerScenarios":"abs(undefined) (+undefined is NaN); abs('abc') (NaN); abs(null) (typeof 'object'); abs({}); abs([1,2]) (object); abs(new Number(-3)) (object wrapper).","commonSituations":"Unvalidated user input; parsing failures producing undefined; values from APIs that arrive as Number objects or numeric strings that aren't actually numeric.","solutions":["Coerce explicitly with Number(num) and validate typeof result === 'number' && !Number.isNaN(result) before calling abs.","Reject object inputs at the boundary; unwrap Number objects with +num only after an isArray/typeof check.","Treat empty string and undefined as invalid rather than letting them become NaN.","If you need Math.abs semantics on primitives, prefer the built-in Math.abs after validation."],"exampleFix":"// before\nconst v = abs(input) // throws on undefined / 'abc' / {}\n\n// after\nconst n = Number(input)\nif (typeof input === 'object' || Number.isNaN(n)) throw new TypeError('num must be a finite number')\nconst v = abs(n)","handlingStrategy":"type-guard","validationCode":"function safeAbs(input) {\n  if (typeof input === 'object') {\n    throw new TypeError('objects are not allowed')\n  }\n  const n = Number(input)\n  if (Number.isNaN(n)) {\n    throw new TypeError('input must be a finite number')\n  }\n  return abs(n)\n}","typeGuard":"const isNumericPrimitive = (v) =>\n  typeof v !== 'object' && v !== null && !Number.isNaN(Number(v))","tryCatchPattern":"try {\n  return abs(value)\n} catch (e) {\n  if (e instanceof TypeError && /nan|not a number/i.test(e.message)) {\n    const n = Number(value)\n    return Number.isNaN(n) ? NaN : Math.abs(n)\n  }\n  throw e\n}","preventionTips":["Coerce with Number(input) and reject NaN before calling abs.","Reject object/null/array inputs at the boundary.","Treat empty string and undefined as invalid.","Unwrap Number objects explicitly rather than passing them through."],"tags":["math","abs","type-error","nan","input-coercion"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}