Array Methods

Array Methods

What is an Array?

An array is a data structure that is used to store different elements in a single variable. Arrays are commonly used in programs to organize data so that a related set of values can be easily sorted or searched.

Array methods are built-in functions in JavaScript. Using these functions, we can manipulate our array.

Array Methods:

1. push(): This method is used to add items to the end of an array.

Example:

const fruits = ['Apple', 'Orange', 'Mango'];
fruits.push('Banana');
console.log(fruits); 
// output: ['Apple', 'Orange', 'Mango','Banana']

2. pop(): This method is used to extract an item from the end of an array.

Example:

const fruits = ['Apple', 'Orange', 'Mango','Banana'] ;
fruits.pop();
console.log(fruits); 
// output: ['Apple', 'Orange', 'Mango']

3. shift(): This method is used to extract an item from the beginning of an array.

Example:

const fruits = ['Apple', 'Orange', 'Mango','Banana'] ;
fruits.shift();
console.log(fruits); 
// output: ['Orange', 'Mango', 'Banana']

4. unshift(): This method is used to add the elements to the beginning of an array:

Example:

const fruits = ['Apple', 'Orange', 'Mango'] ;
fruits.shift('Banana');
console.log(fruits); 
// output: ['Banana', 'Apple', 'Orange', 'Mango']

Methods push() and unshift() can add multiple elements at once.

5. slice(): This method returns a new array, copying to it all items from index start to end (excluding end).

Syntax:

slice()
slice(start,end) // end is not included

Example:

let states = ["Assam", "Bihar", " Goa", "Kerala"];

console.log(slice(2)); 
// output: [" Goa", "Kerala"]
console.log(slice(1,3);
// output: ["Bihar", "Goa"]

6. splice(): This method changes the contents of an array by removing or replacing existing elements and adding new elements in their place.

Example:

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
// output: ["Jan", "Feb", "March", "April", "June"]

7. concat(): This method returns a new array but doesn't change the original one. It merges two or more arrays and gives a new array as an output.

Example:

const arr1 = [0, 5, 10, 15];
const arr2 = [20, 25, 30];
const arr3 = [35,40,45];
const arr4 = arr1.concat(arr2);
const arr5 = arr1.concat(arr2, arr3)
console.log(arr4);       
// output:  [0, 5, 10, 15, 20, 25, 30]
console.log(arr5);    
// output:  [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]

8. indexOf(): This method returns the index of an element in an array. Return "-1" if the element is not found.

Example:

const animals = ['cow', 'goat', 'dog', 'cat'];
console.log(animals.indexOf('dog'));
//  output: 2
console.log(animals.indexOf('giraffe'));
// output: -1

9. fill(): Used this method to change the array with the given static value. It returns the modified array.

Syntax:

fill(value)
fill(value, start)
fill(value, start, end)

Example:

const array = [1, 2, 3, 4];

console.log(array.fill(5, 2, 4));
// output: [1, 2, 5, 5]

console.log(array.fill(10, 1));
// output: [1, 10, 10, 10]

console.log(array.fill(20));
// expected output: [20, 20, 20, 20]

10. filter(): This method returns a new array and removes the element where the condition from the callback function results in true.

Example:

let animals= ['cow', 'goat', 'dog', 'cat'];
animals.filter(f => f !=== 'goat')

console.log(animals);
// output:  ['cow', 'dog', 'cat']

11. map(): This method creates a new array populated with the results of calling a provided function on every element in the calling array.

Example:

const array = [1, 4, 9, 16];

const map1 = array.map(x => x * 2);

console.log(map1);
// output: [2, 8, 18, 32]

12. sort(): This method is used to sort the elements of an array in increasing or decreasing order.

Example:

const animals = ['cow', 'goat', 'dog', 'cat'];
console.log(animals.sort());
// output: ["cat", "cow", "dog", "goat"]

13. forEach(): This method allows running a function for every element of the array.

array = [1,2,3,4];                               
array.forEach(elemt => {console.log(elemt}); 
 // output: 1 2 3 4

14. at(): This method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Example:

const fruits = ['Apples', 'Apricots', 'Avocados', 'Blueberries', 'Blueberries'];

fruits.at(0); 
// ouput: Apples
fruits .at(-1);
 // output: Blueberries
fruits.at(-2); 
//output: Blueberries

15. join(): This method returns a new string by concatenating all of the array’s elements separated by the specified separator.

Example:

arr = [ "hello ","world"];
console.log(arr.join('')); // hello world

So I hope you all have got some idea on JavaScript Array Method's these are the most used methods while using arrays in JavaScript.

Arigato ✌