{"record":{"id":"7fb5f63e35f75454","repo":"krahets/hello-algo","slug":"queue-is-empty-7fb5f6","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js","lineNumber":57,"sourceCode":"        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* Dequeue */\n    pop() {\n        const num = this.peek();\n        // Delete head node\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* Return list for printing */\n    peek() {\n        if (this.size === 0) throw new Error('Queue is empty');\n        return this.#front.val;\n    }\n\n    /* Convert linked list to Array and return */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n\n/* Driver Code */\n/* Access front of the queue element */\nconst queue = new LinkedListQueue();","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js#L39-L75","documentation":"Thrown by peek() on a linked-list-backed queue when size === 0. peek() returns this.#front.val; pop() calls peek() so both share the guard. The check uses this.size (a public count) rather than isEmpty().","triggerScenarios":"Calling queue.pop() or queue.peek() on an empty queue; consuming more than produced; FIFO where front has advanced past the last node.","commonSituations":"BFS with an empty frontier; task/buffer queue drained; loop that pops until falsy but the throw preempts undefined.","solutions":["Check queue.size === 0 (or queue.isEmpty()) before pop/peek.","Drain with while (queue.size > 0) { const v = queue.pop(); ... }.","Track enqueue count to bound a fixed number of pops.","If you must be defensive, catch the error as an end-of-stream signal."],"exampleFix":"// before\nwhile (true) { const v = queue.pop(); ... } // throws when empty\n\n// after\nwhile (queue.size > 0) { const v = queue.pop(); ... }","handlingStrategy":"validation","validationCode":"if (queue.size > 0) {\n  const v = queue.pop();\n} else {\n  // handle empty queue\n}","typeGuard":"function queueHasElements(queue) {\n  return typeof queue.size === 'number' && queue.size > 0;\n}","tryCatchPattern":"try {\n  const v = queue.pop();\n} catch (e) {\n  if (e.message === 'Queue is empty') { /* drained */ }\n  else throw e;\n}","preventionTips":["Check queue.size > 0 before pop/peek.","Drain with while (queue.size > 0) pop().","Track enqueue count to bound a fixed number of pops."],"tags":["queue","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"}