{"record":{"id":"99655d56b74275c5","repo":"TheAlgorithms/JavaScript","slug":"expected-a-number-received-typeof-num","errorCode":null,"errorMessage":"Expected a number, received ${typeof num}","messagePattern":"Expected a number, received (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/SquareRoot.js","lineNumber":11,"sourceCode":"/*\n * Author: Rak Laptudirm\n *\n * https://en.wikipedia.org/wiki/Newton%27s_method\n *\n * Finding the square root of a number using Newton's method.\n */\n\nfunction sqrt(num, precision = 4) {\n  if (!Number.isFinite(num)) {\n    throw new TypeError(`Expected a number, received ${typeof num}`)\n  }\n  if (!Number.isFinite(precision)) {\n    throw new TypeError(`Expected a number, received ${typeof precision}`)\n  }\n  let sqrt = 1\n  for (let i = 0; i < precision; i++) {\n    sqrt -= (sqrt * sqrt - num) / (2 * sqrt)\n  }\n  return sqrt\n}\n\nexport { sqrt }\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/SquareRoot.js#L1-L24","documentation":"Thrown by the `sqrt(num, precision)` function (Newton's method square root) when `num` fails `Number.isFinite(num)`. The guard rejects NaN, +/-Infinity and every non-number type because the iterative refinement `sqrt -= (sqrt*sqrt - num)/(2*sqrt)` would otherwise divide by zero or propagate NaN. Pass a finite JavaScript number to clear the check.","triggerScenarios":"Call `sqrt(undefined)`, `sqrt(null)`, `sqrt('16')`, `sqrt(NaN)`, `sqrt(Infinity)`, `sqrt({})`, `sqrt([4])`, or any value where `Number.isFinite` returns false as the first argument.","commonSituations":"Reading numeric values from HTML forms, CLI args or env vars (all strings), propagating an optional field that was never set, or piping output of a previous computation that returned NaN on bad input.","solutions":["Coerce before calling: pass `Number(value)` and verify `Number.isFinite` first.","If the source is a string from user input, parse explicitly with `parseFloat(value)` and validate the result.","Provide an explicit default at the call site so `undefined` never reaches the function."],"exampleFix":"// before\nsqrt(formValue) // formValue is a string like '16'\n\n// after\nconst n = Number(formValue)\nif (!Number.isFinite(n)) throw new TypeError('formValue must be a finite number')\nsqrt(n)","handlingStrategy":"type-guard","validationCode":"const n = Number(num)\nif (!Number.isFinite(n)) {\n  throw new TypeError('num must be a finite number')\n}\nsqrt(n)","typeGuard":"const isFiniteNumber = (x) => typeof x === 'number' && Number.isFinite(x)","tryCatchPattern":null,"preventionTips":["Always coerce external input with Number() before passing to numeric APIs.","Use Number.isFinite rather than typeof === 'number' - it excludes NaN and Infinity.","Treat the boundary layer (forms, JSON, CLI) as untrusted and validate there."],"tags":["type-validation","math","input-validation","newton-method"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}