Array Methods in JavaScript for adding and removing items from the beginning and end of a list

 Methods that use arrays

Arrays have a plethora of methods. They are divided into groups in this chapter to make things easier.

Items can be added or removed.

We already have methods for adding and removing items from the beginning and end of a list:

arr.push(...items) – pushes things to the end of the list.

  • arr.pop() – pulls a single item from the end of the array.

  • arr.shift() – extracts a single item from the start.
arr.unshift(...things) – inserts items at the start.

const arr = ['🍅', '🍄', '🥦', '🥒', '🌽', '🥕', '🥑'];

Array Methods in JavaScript
1. map( ) This method returns a new array with the results of running a function on each member of the array.

const newArr = numbers.map; const numbers = [4, 9, 16, 25]; (Math.sqrt)

2. filter( )
This method produces a new array containing only elements that satisfy the criteria in the function.
This function is used to sort the items of an array in either ascending or descending order.

4. forEach( )
This technique aids in looping through an array by calling a callback function for each element in the array.

5. concat( )
 This function joins two or more arrays and returns a new array without modifying the originals.

Comments

Popular posts from this blog

The Complete Guide to Reacting for Beginners in 2021

Complete JSON tutorial: learn JSON quickly