Array Methods simplified for beginners

Array methods in javascript

Hi Folks,

As a JavaScript developer, it is essential to understand the most common data structure, which is an array. Array methods help developers write short and clean code, especially avoiding unnecessary codes like looping.

In this article, I am going to explain array methods to you in the simplest way i can.

For better understanding, I will divide them into categories of methods that have similar functions.

• Methods that add or remove objects

• Methods that iterate and return a new array

• Methods that return boolean values

• Array methods that return a value

  • Methods that add or remove items The array methods we would be looking at in this category include pop, push, shift, unshift. To show how each method works, we are going to use the sample array below;

    let  food = [‘rice’, ‘yam’, ‘beans’, ‘bread’, ‘pasta’]
    

• Array.push: the array method push, adds an item to the end of the array. Example;

image.png

• Array.pop: this removes an item from the end of an array

image.png

• Array.unshift: adds an item to the beginning of an array

image.png

• Array.shift: removes an item from the beginning of an array.

image.png

  • Methods that iterate and return another array

• Array.map – the map( ) method returns an array after executing a function assigned to it. Example;

image.png In this example, we used an arrow function, x.

• Array.filter: the filter( ) method returns a new array of elements that meet a certain condition based on a given function. E.g

image.png

• Array.forEach: this method executes a function for every single element in the array

image.png

  • Methods that return Boolean values.

• Array.every: this returns a Boolean value true, if all items in the array meet a certain condition, else it returns false. .

image.png • Array.some: this returns true if it finds an element that matches a condition, and returns false if it doesn’t.

image.png

  • Array methods that return a value

• Array.find: this method allows you to quickly find an element in your array. If the element you are looking for has the same name as another, it stops as soon as it finds the first one.

image.png

• Array.findIndex: method allows you to quickly find the index of an element in your array.

image.png

Thank you for reading, Kindly check my other articles and follow me here & on twitter (@jovialcoderr)