JavaScript Array Methods

There are a number of methods that can be used to work with arrays in JavaScript. Some of the most common methods are:

  • push(): Adds a value to the end of the array.
  • pop(): Removes the last value from the array.
  • unshift(): Adds a value to the beginning of the array.
  • shift(): Removes the first value from the array.
  • indexOf(): Returns the index of a value in the array.
  • length: Returns the length of the array.

Let’s take an example array of colors that contains three string values to understand each of the above methods:

let colors = ["red", "green", "blue"];

				

Task

Run the above code to understand the different methods of an array and experiment with the code to get used to it.

ADVERTISEMENT
ADVERTISEMENT