Posted on

Using basic array functions, you can treat an array as a queue (first in first out) or a stack (last in first out):

To act as a queue, use the shift function:

while (arr.length) {
let val = arr.shift();
console.log(val);
}

To act as a stack, use the pop function:

while (arr.length) {
let val = arr.pop();
console.log(val);
}

Leave a Reply

Your email address will not be published.