I coded up a basic DLL for use in a stack or a queue in JavaScript for practice. Coming from a C++/Java background, I basically just converted how I would code it up in one of those. I'm not sure if there's a more JavaScript optimized way of doing it.

function Node(val) {
this.data = val;
this.prev = null;
this.next = null;
}

this.tail = null;

if (this.head === null) {  //If first node
} else {
var temp = new Node(val);
}
};

if (this.tail === null) {  //If first node
this.tail = new Node(val);
} else {
var temp = new Node(val);
temp.prev = this.tail;
this.tail.next = temp;
this.tail = temp;
}
};

var toReturn = null;

this.tail = null;
} else {
}
}
};

this.removeAtTail = function () {
var toReturn = null;

if (this.tail !== null) {
toReturn = this.tail.data;

this.tail = null;
} else {
this.tail = this.tail.prev;
this.tail.next = null;
}
}

};

this.each = function (f) {
while (curr !== null) {
f(curr);
curr = curr.next;
}
};

this.printList = function () {
this.each(function (item) {
console.log(item.data);
});
};
}

var runTests = function () {

testList.printList();

testList.removeAtTail();

testList.printList();

testList.printList();

};

• If you're doing it as an exercise, there are certainly ways to reduce code size and make it more "JavaScripty". However, if you're doing a linked list to optimize algorithms that generally don't work too well on arrays (which is all you got in JS), you'll probably be disappointed. Mar 7, 2015 at 20:02
• stackoverflow.com/questions/22280282/… Mar 7, 2015 at 20:09

Overall, your code looks very nice. I just have a few, very small things to point out.

The value null is called a falsey value. This means that null can be treated as though it were false.

That being said, conditionals like this:

this.head === null


and this

this.tail !== null


can be shortened to this:

!this.head


and this

this.tail


respectively.

If you don't understand why, or want to experiment around a bit more, just open up your web browser's developer console and enter this:

var foo = null;


You will not get a notification saying "bar". However, if you write

if(!foo) { ... }


You will get a notification saying "bar".

I think it is a bad idea to, in the function printList, to log out to the browser's console.

1. Different browsers have different implementations of console, which is why console.log is mainly used for debugging.

2. What if the user does not want to log to the console?

I think that, instead of logging out to the console, you should return a string of all the nodes' values. That way, the person who is using this code that you wrote can decide how they want to handle output of these values.

That would make your method look something like this:

this.printList = function() {
var dataString = "";
this.each(function(item) {
dataString += item.data + "\n";
});
return dataString;
}


In this case, you may want to change the name from printList.

You should be setting the methods of LinkedList through the prototype, not through the "constructor" of the class.

What I mean is, you should be doing this:

function LinkedList() {
this.tail = null;
}

...
}

...


rather than this:

function LinkedList() {
this.tail = null;

...
}
...
}


Read here and here for why.

This is just a tip to extend on your implementation.

I recommend that you add some optional parameters to the LinkedList class constructor: someone using this may already have a some Nodes that they'd like to use in a new LinkedList.

Here is how I would change the constructor:

function LinkedList(head, tail) {

The same goes to your Node constructor; maybe the user already has a next and a prev.