Array Methods

JavaScript array methods

Array Methods

What is Array.

Arrays are used to store multiple values in a single variable. This is compared to a variable that can store only one value. Each item in an array has a number attached to it, called a numeric index, that allows you to access it. In JavaScript, arrays start at index zero and can be manipulated with various methods.

Example:-

const Arr1 = ["Mango", "Apple", "Orange", "Plum"]
console.log(Arr1)

Output:-

 ["Mango", "Apple", "Orange", "Plum"]

Array Methods.

  • concat() :-

The concat() method is used to merge two or more arrays.

Example :-

const arr1 = ["Apple","Orange"]
const arr2 = ["Cake","Candy","Apple Juice"]

console.log(arr1.concat(arr2))

Output :-

["Apple","Orange","Cake","Candy","Apple Juice"]
  • slice() :-

The slice() method returns selected elements in an array, as a new array. The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.

Example :-

const arr1 = ["Butter", "Cheese" , " Salad", "Pizza"]

console.log(arr1.slice(1,3))

Output :-

 [ 'Cheese', ' Salad' ]
  • splice() :-

The splice method we can change of an array by removing or replacing existing elements and/or adding new elements in a place.

Example : -

const arr1 = ["Butter", "Cheese", " Salad", "Pizza"]
            arr1.splice(2,0, "sandwich", "eggs" )
console.log(arr1)

Output :-

["Butter", "Cheese", "sandwich", "eggs"," Salad", "Pizza"]

Remove the elements in index 2 and items 1

const arr1 = ["Butter", "Cheese", " Salad", "Pizza"]
            arr1.splice(2,1, "sandwich", "eggs" )
console.log(arr1)

Output :-

["Butter", "Cheese", "sandwich", "eggs", "Pizza"]
  • sort() :-

The sort method is used to sort the array of elements sorted in ascending / descending order.

Example :-

const arr1 = ["Cheese", "Pizza", "Butter", " Salad"]
             arr1.sort()
console.log(arr1)

Output :-

[' Salad', 'Butter', 'Cheese', 'Pizza']
  • map() :-

Map is a collection of elements where each element is stored as a Key, value pair.

Example :-

const arr1 = ["Cheese", "Apple", "Butter", " Salad"]
arr1.map((value,index)=>{
    console.log(value)
})

Output :-

Cheese
Apple
Butter
Salad
  • forEach() :-

The forEach() method calls a function and iterates over the elements of an array. The forEach() method can also be used on Maps and Sets.

Example :-

const arr1 = ["Cheese", "Pizza", "Butter", " Salad"]
arr1.forEach((val)=>{
        console.log(val)
})

Output :-

Cheese
Pizza
Butter
Salad
  • reverse() :-

The array of reverse method reverses the element of an array. The first array elements becomes the last and last becomes the first elements.

Example :-

const arr1 = ["Cheese", "Pizza", "Butter", " Salad"]
console.log(arr1.reverse());

Output :-

[ ' Salad', 'Butter', 'Pizza', 'Cheese' ]
  • indexOf() :-

    1. Searching String indexOf() :-
      The JavaScript string indexOf() method is used to search the position of a particular character or string in a sequence of given char values.

      The index position of first character in a string is always starts with zero. If an element is not present in a string, it returns -1.

Example :-

const arr1 = "Hey this is the complete array methods"
const value = arr1.indexOf("the")
console.log(value)

Output :-

12
  1. Searching Array indexOf() :-
    The JavaScript array indexOf() method is used to search the position of a particular element in a given array.

    The index position of first element in an array is always start with zero. If an element is not present in an array, it returns -1.

Example :-

const arr1 = ["Cheese", "Pizza", "Butter", " Salad"]
console.log(arr1.indexOf("Butter"))

Output :-

2
  • copyWithin() :-

The array of copyWitnin method the copies the part of the given array with its own elements and returns the modified array. This method doesn't change the length of the modified array.

Example :-

const arr1 = ["Cheese", "Pizza", "Butter", " Salad"]
console.log(arr1.copyWithin(3,0));
console.log(arr1)

Output :-

[ 'Cheese', 'Pizza', 'Butter', 'Cheese' ]
[ 'Cheese', 'Pizza', 'Butter', 'Cheese' ]