TheAlgorithms/JavaScript · warning · TypeError
Array is empty
Error message
Array is empty
What it means
Thrown by findMin(...nums) (FindMin.js:9) as a TypeError when the function is called with no arguments at all. findMin is variadic (uses rest parameters), so calling it with zero arguments yields nums.length === 0 and there is no minimum to compute. Note this is thrown as TypeError even though it is semantically an empty-input condition. Calling findMin(5) with a single argument is valid and returns 5.
Source
Thrown at Maths/FindMin.js:10
/**
* @function FindMin
* @description Function to find the minimum number given in an array of integers.
* @param {Integer[]} nums - Array of Integers
* @return {Integer} - The minimum number of the array.
*/
const findMin = (...nums) => {
if (nums.length === 0) {
throw new TypeError('Array is empty')
}
let min = nums[0]
for (let i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i]
}
}
return min
}
export { findMin }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Guard the call: if (values.length === 0) return undefined (or Infinity) before spreading.
- Pass the array directly if you have one, rather than spreading: write your own loop or use Math.min for variadic cases.
- Provide a fallback at the call site: const m = values.length ? findMin(...values) : Infinity.
- Validate the upstream source produced at least one value before computing the min.
Example fix
// before const m = findMin(...values) // throws if values is [] // after const m = values.length === 0 ? Infinity : findMin(...values)
Defensive patterns
Strategy: validation
Validate before calling
if (!Array.isArray(values) || values.length === 0) {
return Infinity // or a domain-appropriate default
}
const m = findMin(...values) Type guard
const isNonEmptyNumberArray = (v) => Array.isArray(v) && v.length > 0 && v.every(x => typeof x === 'number')
Try / catch
try {
m = findMin(...values)
} catch (e) {
if (e instanceof TypeError && e.message === 'Array is empty') {
m = Infinity
} else throw e
} Prevention
- Guard against empty arrays before spreading into the variadic call.
- findMin is variadic — prefer Math.min for a single array argument to avoid spread pitfalls.
- Note this empty-input condition is thrown as TypeError, unlike mean's plain Error.
When it happens
Trigger: Call findMin() with no arguments; call findMin(...emptyArray) where the spread yields zero args; call findMin.apply(null, []) with an empty array via apply.
Common situations: Spreading a dynamically-sized array that happens to be empty; min computation over a filtered result that retained nothing; a variadic call site that assumed at least one argument would always be present.
Related errors
- Array is empty
- Input is not a valid 2D matrix.
- Index out of range
- The param should be a nonempty string
- Provided input is not an array
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/f69a18992633c5a2.
Report an issue: GitHub.