You should test current , not current.next :
obj[Symbol.iterator] = function() { var current = this; return { next() { if (current !== null) { var res = {value: current.value, done: false}; current = current.next; return res; } else { return {done: true}; } } }; }
But he can do it a lot easier than the generator method:
obj[Symbol.iterator] = function* () { for (var current = this; current !== null; current = current.next) { yield current.value; } }
Btw, I would recommend not putting this iterator on each node of the list (or even on the first). Insert a separate object that points to the head of the list, or make it a static helper function:
let list = { head: obj, // could be null either *[Symbol.iterator]() { for (var current = this.head; current !== null; current = current.next) { yield current.value; } } }
function* linkedList(head) for (; head !== null; head = head.next) { yield head.value; } }
source share