Array Methods in JavaScrtiptArray Methods in JavaScrtipt

These are some useful array methods in JavaScript

  • Array length
  • Array forEach()
  • Array reverse()
  • Array indexOf()
  • Array sort()
  • Array fill()
  • Array join()
  • Array toString()
  • Array concat()
  • Array slice()

Array length

The length property returns the length of an array.

// Example of length property
let arr = ['Recursive', 'Minds', 1, 2];

// It will print length of arr
console.log(arr.length);
// Output
4

 

Array forEach()

The forEach is used to iterate over elements of an array. forEach() the function takes callback function lets understand using an example

// Example of forEach() method
let arr = [1, 2, 3, 4, 5];

// It will print all the elements of array
arr.forEach((element) =>{
    console.log(element);
})
// Output
1
2
3
4
5

 

Array reverse()

The reverse() method returns the array in reverse order. For example

// Example of reverse() method
let arr = [1, 2, 3, 4, 5];

// It will reverse order of array element
arr.reverse();
console.log(arr);
// Output
[5, 4, 3, 2, 1]

 

Array indexOf()

The indexOf() method returns the index of given element. For example

// Example of indexOf() method 
let arr = [1, 2, 3, 4, 5];

// It will print index of 4 
console.log(arr.indexOf(4));
// Output 
4

Array sort()

The sort() methods sort the array elements in a specific order (Ascending order or descending order). For example

// Example of reverse() method 
let arr = [1, 5, 3, 4, 2];

// It will sort the arr into ascending array
arr.sort();
console.log(arr);
// Output
[1, 2, 3, 4, 5]

 

Array fill()

The fill() method returns the array by filling array elements with a specific value. Let’s understand with an example

// Example of reverse() method 
let arr = [1, 2, 3, 4, 5];

// It will replace all elements with 10
arr.fill(10);
console.log(arr);
// Output
[10, 10, 10, 10, 10]

 

Array join()

The join method returns a new string by concatenating all elements of an array by a specified separator. For example

// Example of join() method in array
let arr = ['Recursive', 'Minds', 'by', 'Amit', 'Kumar'];

// It will return string of all elements seprated by single space
let ans = arr.join(' ');
console.log(ans);
// Output
"Recursive Minds by Amit Kumar"

 

Array toString()

The toString() method returns a string of elements of an array. For example

// Example of toString() method in array
let arr = ['Recursive', 'Minds', 'by', 'Amit', 'Kumar'];

// It will convert all elements to string
let ans = arr.toString();
console.log(ans);
// Output
"Recursive,Minds,by,Amit,Kumar"

 

Array concat()

The concat() method is used to merge two arrays and it returns a new array. For example

// Example of concat()
let arr1 = [1,2,3,4,5];
let arr2 = [6,7,8,9,10];

// It will merge arr1 and arr2 into single array
let mergedArr = arr1.concat(arr2);
console.log(mergedArr);
// Output
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

 

Array slice()

The slice() method returns a specified portion of the array in the form of a new array. Let’s understand using example

// Example of slice() method in array
let arr = [1, 2, 3, 4, 5, 6, 7, 8];

// It will create another array by slicing numbers from index 2 to 5
let newArray = numbers.slice(2, 6);
console.log(newArray);
// Output
[3, 4, 5, 6]

 

Thank you so much for reading

Happy Coding

For more details visit MDN Docs

Some other articles on JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *