{"record":{"id":"f69a18992633c5a2","repo":"TheAlgorithms/JavaScript","slug":"array-is-empty-f69a18","errorCode":null,"errorMessage":"Array is empty","messagePattern":"Array is empty","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"Maths/FindMin.js","lineNumber":10,"sourceCode":"/**\n * @function FindMin\n * @description Function to find the minimum number given in an array of integers.\n * @param {Integer[]} nums - Array of Integers\n * @return {Integer} - The minimum number of the array.\n */\n\nconst findMin = (...nums) => {\n  if (nums.length === 0) {\n    throw new TypeError('Array is empty')\n  }\n\n  let min = nums[0]\n  for (let i = 1; i < nums.length; i++) {\n    if (nums[i] < min) {\n      min = nums[i]\n    }\n  }\n\n  return min\n}\n\nexport { findMin }\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/FindMin.js#L1-L24","documentation":"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.","triggerScenarios":"Call findMin() with no arguments; call findMin(...emptyArray) where the spread yields zero args; call findMin.apply(null, []) with an empty array via apply.","commonSituations":"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.","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."],"exampleFix":"// before\nconst m = findMin(...values) // throws if values is []\n\n// after\nconst m = values.length === 0 ? Infinity : findMin(...values)","handlingStrategy":"validation","validationCode":"if (!Array.isArray(values) || values.length === 0) {\n  return Infinity // or a domain-appropriate default\n}\nconst m = findMin(...values)","typeGuard":"const isNonEmptyNumberArray = (v) => Array.isArray(v) && v.length > 0 && v.every(x => typeof x === 'number')","tryCatchPattern":"try {\n  m = findMin(...values)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Array is empty') {\n    m = Infinity\n  } else throw e\n}","preventionTips":["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."],"tags":["validation","variadic","empty-input","array","edge-case"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}