In this class we reviewed the difference between an array and a linked list, the advantages and disadvantages of each, and their implementation.

Implementation using a simple Javascript Array:
//Stack simple implementation using a *javascript* array
 
class Stack {
    constructor() {
        this.stack = [];
    }
 
    push(value) {
        this.stack.push(value);
    }
 
    pop() {
        //null handling
        if ((this.stack.length = 0)) {
            return "this stack is empty";
        }
        this.stack.pop();
    }
 
    peek() {
        if ((this.stack.length = 0)) {
            return "stack is empty";
        }
        return this.stack[this.stack.length - 1];
    }
}
Implementation using a linked list:
//Stack using Linked List
class Node {
    constructor(value) {
        this.value = value;
        this.prev = null; 
    }
}
 
class Stack {
    constructor() {
        this.head = null; 
        this.length = 0;
    }
 
    push(item) {
        const node = new Node(item);
        this.length++;
        //null handling
        if (!this.head) {
            this.head = node;
            return;
        }
        node.prev = this.head;
        this.head = node;
    }
 
    pop() {
        if (this.length === 0) {
            return null; 
        }
        this.length = Math.max(0, this.length - 1);
        const head = this.head;
        this.head = head.prev;
        return head.value;
    }
 
    peek() {
        return this.head ? this.head.value : null; 
    }
}