TheAlgorithms/JavaScript · error · TypeError

Argument is NaN - Not a Number

Error message

Argument is NaN - Not a Number

What it means

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).

Source

Thrown at Maths/Abs.js:16

/**
 * @function abs
 * @description This script will find the absolute value of a number.
 * @param {number} num - The input integer
 * @return {number} - Absolute number of num.
 * @see https://en.wikipedia.org/wiki/Absolute_value
 * @example abs(-10) = 10
 * @example abs(50) = 50
 * @example abs(0) = 0
 */

const abs = (num) => {
  const validNumber = +num // converted to number, also can use - Number(num)

  if (Number.isNaN(validNumber) || typeof num === 'object') {
    throw new TypeError('Argument is NaN - Not a Number')
  }

  return validNumber < 0 ? -validNumber : validNumber // if number is less than zero means negative, then it converted to positive. i.e., n = -2 = -(-2) = 2
}

export { abs }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Coerce explicitly with Number(num) and validate typeof result === 'number' && !Number.isNaN(result) before calling abs.
  2. Reject object inputs at the boundary; unwrap Number objects with +num only after an isArray/typeof check.
  3. Treat empty string and undefined as invalid rather than letting them become NaN.
  4. If you need Math.abs semantics on primitives, prefer the built-in Math.abs after validation.

Example fix

// before
const v = abs(input) // throws on undefined / 'abc' / {}

// after
const n = Number(input)
if (typeof input === 'object' || Number.isNaN(n)) throw new TypeError('num must be a finite number')
const v = abs(n)
Defensive patterns

Strategy: type-guard

Validate before calling

function safeAbs(input) {
  if (typeof input === 'object') {
    throw new TypeError('objects are not allowed')
  }
  const n = Number(input)
  if (Number.isNaN(n)) {
    throw new TypeError('input must be a finite number')
  }
  return abs(n)
}

Type guard

const isNumericPrimitive = (v) =>
  typeof v !== 'object' && v !== null && !Number.isNaN(Number(v))

Try / catch

try {
  return abs(value)
} catch (e) {
  if (e instanceof TypeError && /nan|not a number/i.test(e.message)) {
    const n = Number(value)
    return Number.isNaN(n) ? NaN : Math.abs(n)
  }
  throw e
}

Prevention

When it happens

Trigger: abs(undefined) (+undefined is NaN); abs('abc') (NaN); abs(null) (typeof 'object'); abs({}); abs([1,2]) (object); abs(new Number(-3)) (object wrapper).

Common situations: Unvalidated user input; parsing failures producing undefined; values from APIs that arrive as Number objects or numeric strings that aren't actually numeric.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/a0abd5b1d38277c8. Report an issue: GitHub.