TheAlgorithms/JavaScript · error · TypeError
Invalid Input
Error message
Invalid Input
What it means
Thrown by mean(numbers) (AverageMean.js:14) as a TypeError when the argument is not an array (Array.isArray returns false). This is the first of three validation checks in mean: non-array throws here, empty array throws separately, and non-number elements throw inside the loop. The function expects a single array argument, not spread/rest arguments.
Source
Thrown at Maths/AverageMean.js:14
/**
* @function mean
* @description This script will find the mean value of a array of numbers.
* @param {number[]} numbers - Array of integer
* @return {number} - mean of numbers.
* @throws {TypeError} If the input is not an array or contains non-number elements.
* @throws {Error} If the input array is empty.
* @see [Mean](https://en.wikipedia.org/wiki/Mean)
* @example mean([1, 2, 4, 5]) = 3
* @example mean([10, 40, 100, 20]) = 42.5
*/
const mean = (numbers) => {
if (!Array.isArray(numbers)) {
throw new TypeError('Invalid Input')
} else if (numbers.length === 0) {
throw new Error('Array is empty')
}
let total = 0
numbers.forEach((num) => {
if (typeof num !== 'number') {
throw new TypeError('Invalid Input')
}
total += num
})
return total / numbers.length
}
export { mean }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Wrap the arguments in an array literal: mean([1, 2, 3]) not mean(1, 2, 3).
- Convert array-likes with Array.from() or spread: mean([...mySet]).
- Guard the call: if (!Array.isArray(x)) return NaN or convert.
- If the value may be undefined from optional chaining, default it: mean(data ?? []).
Example fix
// before const m = mean(...values) // spreads into separate args, not an array // after const m = mean(Array.isArray(values) ? values : [values]) // or simply: const m = mean(values) // pass the array directly
Defensive patterns
Strategy: type-guard
Validate before calling
if (!Array.isArray(data)) {
data = Array.from(data) // for iterables, or wrap single values
}
const m = mean(data) Type guard
const isNumberArray = (v) => Array.isArray(v) && v.every(x => typeof x === 'number')
Try / catch
try {
m = mean(numbers)
} catch (e) {
if (e instanceof TypeError && e.message === 'Invalid Input') {
// not an array — convert and retry
} else throw e
} Prevention
- mean takes a single array argument, not spread args — never call mean(1,2,3).
- Convert Sets/Maps with Array.from before passing.
- Default optional inputs with ?? [] so undefined never reaches the function.
When it happens
Trigger: Call mean(1, 2, 3) (passing spread args instead of an array); mean('123') passing a string; mean({length: 3}) passing an array-like object; mean(42) passing a scalar; mean(null) from a missing field.
Common situations: Confusing mean([1,2,3]) with mean(1,2,3) — the function is not variadic; passing a Set or Map that must be spread to an array first; passing arguments object from a legacy function; receiving undefined from a failed JSON lookup.
Related errors
- Invalid Input
- Argument must be an Array
- Invalid Input
- The ${paramName} should be type Number
- Type of n must be number
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/e302af1f96f0c73d.
Report an issue: GitHub.