From the course: JavaScript: Arrays

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

The beginning: Unshift and shift data

The beginning: Unshift and shift data - JavaScript Tutorial

From the course: JavaScript: Arrays

The beginning: Unshift and shift data

- A queue functions similarly to a stack. But this data structure operates under the first in, first out rule. In order to add or remove data from a queue, we will use the array methods, unshift and shift. Unshift and shift operate almost identically to push and pop except that the data is added and removed from the beginning of the array versus the end. The array method unshift allows us to add one or more items to the beginning of an array. Here we will use our sports array again, but this time we're going to pass baseball to unshift. As we can see, baseball was added to the beginning of the sports array. The unshift array method takes a single argument, which is whatever you'd like to add to your existing array. It can be one or more elements. Unshift returns the length property for the newly changed array. This is the same behavior as the array method push. Now let's move on to shift. When this array method is applied…

Contents