Queue and Stack in javascript

Posted by Joe Enos on June 23, 2021 · 1 min read

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);
}