HTML and CSS Reference
In-Depth Information
The net result of this code is exactly the same as for the pop and push code, except the
operations occur at the front of the array instead of the end. In other words, this example still
illustrates the stack functionality of “last in, first out.”
Taken together, the two concepts you've just seen ( pop / push and shift / unshift ) can be
combined to create the concept of a first-in-first-out queue. The following code demonstrates
using a queue in which the front of the line is the beginning of the array and the end of the
line is the end of the array:
var sports = new Array();
var get1 = sports.shift();
var get2 = sports.shift();
This code first pushes some items into the array. This means that each item is added to the
end of the array. When an item is needed from the array, the shift method gets the first item
out of the beginning of the array—the item at index 0. You can easily implement the opposite
mechanism by using the unshift and pop methods, which would achieve the same results but
enter and retrieve items from the opposite ends of the array from this example.
Using advanced array methods
This section examines some of the more advanced array methods. These methods all involve
the use of a callback , which you'll examine in more detail in Objective 2.4, “Implement a call-
back.” If callbacks are a new concept to you, you should study that objective before complet-
ing this section.
The Array object exposes methods that enable you to process custom logic on every single
element in the array. The following sections demonstrate each method.
Using the every method
The every method lets you process specific logic for each array element to determine whether
any of them meet some condition. Look at following code:
var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12);
var allEven = evenNumbers.every(evenNumberCheck, this);
if (allEven) {
} else {
function evenNumberCheck(value, index, array) {
return (value % 2) == 0;
Search WWH ::

Custom Search