This is a follow-up review to JavaScript queue implementation using linked list

I've run the code through a linter, fixed every style error, added const where it was possible, removed extraneous templates and changed the return policy for calling dequeue() on an empty queue to make it fail fast. I also implemented an empty() method for the user's convenience.

I'm still undecided as to whether to use single or double quotes for strings. The linter suggests single quotes, but I've read reviews suggesting double quotes might be better because they look less like backticks, which are used for template literals.

Is there anything I've missed?

function makeQueue() {
  // data
  let head = null;
  let size = 0;

  // methods
  function enqueue(x) {
    let newNode = {next: head, data: x};
    head = newNode;

  function dequeue() {
    if (size === 0) {
      throw new Error('dequeue() on an empty queue');

    const x = head.data;
    head = head.next;

    return x;

  function getSize() {
    return size;

  function isEmpty() {
    return size === 0;

  // public
  return {
    size: getSize,
    empty: isEmpty

var q0 = makeQueue();
var q1 = makeQueue();
var q2 = makeQueue();

for (var i = 0; i < 16; i++) {



The goal here was to improve the JavaScript, I only realized now that I actually wrote a LIFO stack.


Looks good!

I'm a bit surprised the linter didn't catch redundant semicolons at the end of some function declarations, for example here:

  function getSize() {
    return size;

I would rename all the local variables and function parameters named x to data.

In enqueue, you could inline the newNode variable, and assign the object literal {next: head, data: data} directly to head.

It would be good to test a bit more thoroughly, for example dequeue everything from a non-empty queue, and performing a mix of enqueue and dequeue operations.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.