JavaScript Array and 18 must know array methods with example.

Md Readwan
2 min readNov 2, 2020

what is an array? and, why need an array when we have variable?

Variable is used when we need to store a single element. But, what if we need to store multiple elements? We can’t store multiple elements in a variable. At that time array comes.

Array is a collection of multiple elements and every element have an index. By using index we can retrieve elements from the array and we can use those elements as our need. In javascript, we can store the nonhomogenous elements in an array but don't do it. Store homogeneous data in an array is recommended, it helps you to retrieve elements.

Let's assume every one of us knows how to declare and retrieve elements from an array. Now we will learn 18 must-know javascript array methods.

1. concat()

Concate is used for marge two or more array.

Example of concat() method

2. every()

This method is used to check all of the elements present in array weather it means the provided condition or not. If every element means the condition then it returns true otherwise return false.

Example of every() method

3. filter()

Filter method is used to create a new array with all of the elements that means the provided condition.

Example of filter() method

4. find()

Almost the same as filter method but find return only the first element that means the provided condition.

Example of find() method

5. findIndex()

findIindex is Similar as find method, Where find returns the condition matched element, and here findIndex returns the condition matched elements index.

Example of findIndex() method

6. forEach()

This method executes the provided function for each element present in the array at once.

Example of forEach() method

7. includes()

This method finds a particular element is present in an array or not.

Example of includes() method

8. indexOf()

indexOf returns the index if found the element that search by the condition in the array otherwise return -1.

Example of indexOf() method

9. map()

This method executes the provided function for each element present in the array at once.

Example of map() method

10. pop()

Pop removes the last element from the array.

Example of pop() method

11. push()

Add element at the end of the array.

Example of push() method

12. shift()

Shift removes an element from the beginning of an array.

Example of shift() method

13. unshift()

Unshift adds an element at the beginning of an array.

Example of unshift() method

14. toString()

toString method return array elements as a string.

Example of toString() method

15. reverse()

This method reverses an array, where first becomes the last element of the array.

Example of reverse() method

16. sort()

Sort method sorts the array elements in ascending order.

Example of sort() method

17. slice()

This method slice an array from the given start index to the end index, where end index is not included.

Example of slice() method

18. splice()

Splice method adds specific elements by removing specific elements.

Example of splice() method

--

--