{"record":{"id":"bca9f4d6c6dec9e3","repo":"krahets/hello-algo","slug":"stack-is-empty","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_stack_and_queue/array_stack.js","lineNumber":31,"sourceCode":"\n    /* Get the length of the stack */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* Check if the stack is empty */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* Push */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* Pop */\n    pop() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack.pop();\n    }\n\n    /* Return list for printing */\n    top() {\n        if (this.isEmpty()) throw new Error('Stack is empty');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* Return Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n\n/* Driver Code */\n/* Access top of the stack element */\nconst stack = new ArrayStack();","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_stack_and_queue/array_stack.js#L13-L49","documentation":"Thrown by pop() on an array-backed stack when the stack is empty (length === 0). pop() guards before delegating to Array.pop so it never returns undefined silently. The check uses isEmpty() which tests this.#stack.length === 0.","triggerScenarios":"Calling stack.pop() on a freshly created or fully drained stack; unbalanced push/pop pairs; recursive-emulation loops that pop past the base.","commonSituations":"Expression-evaluation / parenthesis matching that pops on unexpected input; undo stacks drained by redo; DFS emulation popping an empty frontier.","solutions":["Check stack.isEmpty() before pop().","In a loop: while (!stack.isEmpty()) { const v = stack.pop(); ... }.","Validate input grammar so pops are always preceded by matching pushes.","Wrap pop() in a helper returning a default when empty if that is valid for your domain."],"exampleFix":"// before\nconst v = stack.pop(); // throws when empty\n\n// after\nconst v = stack.isEmpty() ? null : stack.pop();","handlingStrategy":"validation","validationCode":"if (!stack.isEmpty()) {\n  const v = stack.pop();\n} else {\n  // handle empty stack\n}","typeGuard":"function stackHasElements(stack) {\n  return typeof stack.isEmpty === 'function' && !stack.isEmpty();\n}","tryCatchPattern":"try {\n  const v = stack.pop();\n} catch (e) {\n  if (e.message === 'Stack is empty') { /* drained */ }\n  else throw e;\n}","preventionTips":["Check isEmpty() before pop().","Drain with while (!stack.isEmpty()) pop().","Ensure push/pop pairs are balanced in your control flow."],"tags":["stack","empty-state","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}