Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Array's methods

Array's contain their own methods for storing, retrieving and manipulating data. Some of the most commonly used are:

1. Push - For adding a new element at the end of an array.

2. splice - adding or removing an element at a specified index position

3. concat - concatenating two arrays

4. indexOf - searches and returns the index of the element that holds this value

5. includes - searches if an element exists in an array and returns a boolean value.

6. Join - Joins all elements of the array into a single string by adding a specified string value in between.

7. pop - Removes the last element of an array and returns it.

8. shift - Removes the first element of the array and shifts the other elements.

9. unshift - Adds an element to the first position. Shift the other ones to a position next.

10. forEach - loops through all elements of an array and performs an action for each element. Accepts a function with three arguments that represent the value of each element, the index of it and the array itself respectively. The operation for each element is performed on the array itself.

11. map - Exactly the same as forEach, the only difference is that after performing the operation for each element, it pushes this element into a new array, at the end map returns a new array, that's why the whole map operation must be assigned into a new variable.

12. filter - accepts a function and filters an existing array based on the condition the function argument specifies. Like map returns a new array.

Find out more array methods here!

Attention: These are not the only array methods. Especially after ES2015 release, arrays come with a bunch of new, advanced and tremendously useful methods. We will see most of them alongside with corresponding problems and exercises inside the EcmaScript 6 section later on.