{"record":{"id":"ab8200d2fb5d7ad0","repo":"TheAlgorithms/JavaScript","slug":"queue-is-empty","errorCode":null,"errorMessage":"Queue is Empty","messagePattern":"Queue is Empty","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Data-Structures/Queue/Queue.js","lineNumber":49,"sourceCode":"\n    if (!this.head && !this.tail) {\n      this.head = node\n      this.tail = node\n    } else {\n      this.tail.next = node\n      this.tail = node\n    }\n\n    return ++this.#size\n  }\n\n  /**\n   * @description - Removes the value at the front of the queue\n   * @returns {*} - The first data of the queue\n   */\n  dequeue() {\n    if (this.isEmpty()) {\n      throw new Error('Queue is Empty')\n    }\n\n    const firstData = this.peekFirst()\n\n    this.head = this.head.next\n\n    if (!this.head) {\n      this.tail = null\n    }\n\n    this.#size--\n\n    return firstData\n  }\n\n  /**\n   * @description - Return the item at the front of the queue\n   * @returns {*}","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Queue/Queue.js#L31-L67","documentation":"Thrown by Queue.dequeue() (Error, not a typed subclass) when the queue is empty. dequeue() first calls peekFirst() which itself throws the same message on an empty queue, so the guard is effectively duplicated; either path produces 'Queue is Empty'.","triggerScenarios":"Calling dequeue() before any enqueue(); calling dequeue() more times than items were enqueued; draining an already-drained queue in a worker loop.","commonSituations":"Producer/consumer where the consumer outruns the producer; processing a batch whose advertised count exceeds enqueued items; reusing a queue after clear without resetting expectations.","solutions":["Call queue.isEmpty() (or check queue.size) before dequeue().","In a drain loop, loop while (!queue.isEmpty()) instead of a fixed count.","Wrap dequeue in try/catch if an empty queue is an expected control-flow signal rather than a bug.","Track enqueued count separately and never dequeue beyond it."],"exampleFix":"// before\nwhile (true) {\n  const item = queue.dequeue() // throws once empty\n  process(item)\n}\n\n// after\nwhile (!queue.isEmpty()) {\n  process(queue.dequeue())\n}","handlingStrategy":"validation","validationCode":"function safeDequeue(queue) {\n  if (queue.isEmpty()) return undefined\n  return queue.dequeue()\n}","typeGuard":"const hasItems = (queue) => !queue.isEmpty()","tryCatchPattern":"try {\n  return queue.dequeue()\n} catch (e) {\n  if (e instanceof Error && /queue is empty/i.test(e.message)) return undefined\n  throw e\n}","preventionTips":["Loop with while (!queue.isEmpty()) instead of a fixed count.","Track enqueued count and never dequeue beyond it.","Treat an empty dequeue as expected control flow only if you wrap it in try/catch by design.","Reset dependent consumers when the queue drains to empty."],"tags":["data-structures","queue","empty-state","fifo"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}