{"record":{"id":"4587a5e957ceda50","repo":"TheAlgorithms/JavaScript","slug":"stack-underflow","errorCode":null,"errorMessage":"Stack Underflow","messagePattern":"Stack Underflow","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Data-Structures/Stack/StackES6.js","lineNumber":30,"sourceCode":"class Stack {\n  constructor() {\n    this.stack = []\n    this.top = 0\n  }\n\n  // Adds a value to the end of the Stack\n  push(newValue) {\n    this.stack.push(newValue)\n    this.top += 1\n  }\n\n  // Returns and removes the last element of the Stack\n  pop() {\n    if (this.top !== 0) {\n      this.top -= 1\n      return this.stack.pop()\n    }\n    throw new Error('Stack Underflow')\n  }\n\n  // Returns the number of elements in the Stack\n  get length() {\n    return this.top\n  }\n\n  // Returns true if stack is empty, false otherwise\n  get isEmpty() {\n    return this.top === 0\n  }\n\n  // Returns the last element without removing it\n  get last() {\n    if (this.top !== 0) {\n      return this.stack[this.stack.length - 1]\n    }\n    return null","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Stack/StackES6.js#L12-L48","documentation":"Thrown by Stack.pop() (plain Error 'Stack Underflow') when this.top === 0, i.e. the stack is empty. The Stack tracks size via a top counter initialized to 0 and incremented by push; pop decrements it, so a pop on an empty stack (top === 0) is rejected rather than returning undefined.","triggerScenarios":"Calling pop() more times than push(); calling pop() on a freshly constructed stack; unbalanced push/pop after an exception path skipped a push.","commonSituations":"Recursion/emulation that pops cleanup frames twice; expression evaluators that over-pop on malformed input; tests popping more samples than they pushed.","solutions":["Guard with stack.isEmpty (or stack.length === 0) before pop().","In drain loops, loop while (!stack.isEmpty) rather than a fixed iteration count.","Ensure every error path still pushes or does not pop an extra time.","Wrap pop in try/catch if underflow is expected control flow (e.g. optional frame)."],"exampleFix":"// before\nconst v = stack.pop() // throws once empty\n\n// after\nconst v = stack.isEmpty ? undefined : stack.pop()","handlingStrategy":"validation","validationCode":"function safePop(stack) {\n  return stack.isEmpty ? undefined : stack.pop()\n}","typeGuard":"const isNonEmpty = (stack) => !stack.isEmpty","tryCatchPattern":"try {\n  return stack.pop()\n} catch (e) {\n  if (e instanceof Error && /underflow/i.test(e.message)) return undefined\n  throw e\n}","preventionTips":["Loop with while (!stack.isEmpty) rather than a fixed pop count.","Audit error paths to ensure no extra pop fires when a push was skipped.","In evaluators, validate token counts before popping operands.","Wrap pop to return undefined when underflow is expected control flow."],"tags":["data-structures","stack","underflow","lifo"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}