DEV Community

Cover image for LIFO, FIFO. The difference between a stack and a queue. A quick guide.
Osania - sancodes
Osania - sancodes

Posted on • Updated on

LIFO, FIFO. The difference between a stack and a queue. A quick guide.

I've been going over some JavaScript concepts lately and recently came across an acronym I hadn't heard before.

Lifo, Fifo. It was easy to remember and described the difference between a stack and a queue perfectly. Queue's and stacks allow you to add or remove items from a data structure in a particular order.

Stacks

Like a stack of plates or these cookies. A stack has a last in, first out order - LIFO. They are stacked one on top of the other. To get to the cookie second from top, you would have to remove the first one. Making it a last in, first out data structure - LIFO.
A stack of cookies

Queue's

A queue has a first in, first out data structure - FIFO.
Like someone standing in line or waiting in a queue. The person who is first in the line, gets seen first.

people standing in line Text

In JavaScript, when you use the

pop() or push() methods

to add or remove something from the end of an array, this process follows the LIFO order.
Removing something from the front of the array, requires a bit more work and can involve looping to ensure when an item is removed from the front, you push all the other elements forward to maintain correct indexing and this process follows the FIFO order.

Hearing this acronym helped me, maybe it can help you too.
Links - I originally heard about this here
& read more about it here & here too

Top comments (2)

Collapse
 
san00 profile image
Osania - sancodes

If you found this useful, let me know. I'd love to hear from you.

Collapse
 
suzeshardlow profile image
Suze Shardlow

Nice explanation! Thank you @san00 ! 💕