The queue methods are similar to stack methods except that instead of working under LIFO principles, they control the array's structure according to FIFO (first-in-first-out) rules.

These methods add items to the end of an array and retrieves from the front of it.

The method that removes the first item in an array is called the shift() method while the one that adds item(s) to the front of an array is called the unshift() method.

In combination with stack methods push() and pop(), these two methods may emulate common queues in both directions as shown in the example:

var cities = new Array();

cities.push("London", "Paris", "Berlin");

alert(cities); // 3 names

var city_1 = cities.shift();

alert(city_1); // "London"

alert(cities); // 2 names

cities.unshift("Kiev");

alert(cities); // 3 names

var city_2 = cities.pop();

alert (city_2); // "Berlin"

Example

The example with arrays queuing methods in JavaScript:

 

›› go to examples ››