{"record":{"id":"617d374fd175023c","repo":"krahets/hello-algo","slug":"stack-is-empty-617d37","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/linkedlist_stack.js","lineNumber":46,"sourceCode":"    /* Push */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* Pop */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (!this.#stackPeek) throw new Error('Stack is empty');\n        return this.#stackPeek.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n\n/* Driver Code */\n/* Access top of the stack element */\nconst stack = new LinkedListStack();","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js#L28-L64","documentation":"Thrown by peek() on a linked-list-backed stack when the internal peak pointer is null/undefined (empty). peek() returns this.#stackPeek.val; pop() calls peek() so both share the guard. The check is a truthiness test on #stackPeek, distinct from the explicit size counter #stkSize.","triggerScenarios":"Calling stack.pop() or stack.peek() on an empty stack; popping after the last node was removed (peak reset to null); unbalanced push/pop.","commonSituations":"DFS emulation with an empty frontier; expression parser peeking an empty operator stack; undo/redo stacks drained.","solutions":["Check stack.isEmpty() (or !stack.peek would throw, so guard on size/isEmpty) before pop/peek.","Drain with while (!stack.isEmpty()) { const v = stack.pop(); ... }.","Ensure every peek/pop is preceded by a matching push in your control flow.","Wrap peek() in a helper returning null when empty if that is a valid state."],"exampleFix":"// before\nconst v = stack.peek(); // throws when empty\n\n// after\nconst v = stack.isEmpty() ? null : stack.peek();","handlingStrategy":"validation","validationCode":"if (!stack.isEmpty()) {\n  const v = stack.peek();\n} else {\n  // handle empty stack\n}","typeGuard":"function stackHasTop(stack) {\n  return typeof stack.isEmpty === 'function' && !stack.isEmpty();\n}","tryCatchPattern":"try {\n  const v = stack.peek();\n} catch (e) {\n  if (e.message === 'Stack is empty') { /* empty */ }\n  else throw e;\n}","preventionTips":["Check isEmpty() before peek()/pop().","Wrap peek() in a helper returning null for empty if that is valid.","Ensure every peek/pop is preceded by a matching push."],"tags":["stack","linked-list","empty-state","javascript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}