LinkList Data Structure Problem

Hello I am trying to learn Link List . I got a tutorial and here it is below

class DyLinklist {
    constructor() {
        this.head = null;
        this.tail = null;
        this.result = null;

    append(value) {

        const newNode = {value: value, next: null};
        if (this.tail) {
   = newNode;
        this.tail = newNode;

        if (!this.head) {
            this.head = newNode
            this.head.prev = null;

    toArray() {
        let elements = [];
        let currentNode = this.head;
        while (currentNode) {
            currentNode =;


        return elements;

let link = new DyLinklist();



I think I dont understand why here this.tail used for? I dont see any implementation of it anywhere except storing value. But If I block it my code does not works. Could anybody please tell me why this.tail used here ? Thank you

the tail property is so you have easier access to the last value in the linked list

console.log(link.tail) //{value: 25, next: null}
console.log( //{value: 25, next: null}

Yes but I never assign it anywhere , so how can I get access from it?

append(value) {

        const newNode = {value: value, next: null};
        if (this.tail) {
   = newNode; 
        this.tail = newNode; //you assign it here

Yeah, I just assigned it here then how it works in the toArray() function ? I never used it , and I am looping on this.head so everything working on this.head

I think you may be confused because the value you are pushing is the whole node rather than the nodes value. The very first thing you push into the array is the head node which is an object that has a reference to effectively all the nodes in the linked list, but because you are pushing it into an array it’s a bit redundant as the method should create an array out of node.value, but what you have is an array with each element being a whole node.

[1, "masum", "karim", true, 78.25, 25];
//this is the array when you push the
//values rather than the whole node

After going over the code more throughly I was pleasantly surprised to see how tail works. In JavaScript things will stay in memory as long as there is something to point towards them, and you can also have two variables point towards the same object, and that would make those variables strictly equal. For the very first node you make in your linked list your head and your tail are pointing towards the same node (the same object), the second time you append a node this if (this.tail) statement will be true, and this = newNode will be executed, and if you remember at this point in time the head and tail point towards the same object in memory so doing this = newNode is the same as doing this = newNode, but after that happens then you do this this.tail = newNode; which makes tail the newest node as well as making it no longer point towards the same node as this.head, and on the third time doing this = newNode; is the same as = newNode;, and this why you need tail. All your nodes excluding the head and tail only exist in memory due to a reference chain created by the head node so if you were to delete the head you would effectively delete everything, but the tail.

Thank you @caryaharper for your valuable explanation and time yes now I got it why tail has been used. it is now clear to me