Introducing JavaScript Array Methods

JavaScript Array Methods

Last Updated on May 7, 2024 by

Array Methods in JavaScript

Array Methods in JavaScript are some built-in methods and properties that simplify operating on Arrays. In JavaScript, arrays are a fundamental data structure that allows you to store multiple elements under a single variable name. There are several built-in methods and properties that simplify operating on arrays.

 

Here are some of the most common array methods in JavaScript:


1. push()
:

The push() method adds one or more elements to the end of an array and returns the new length of the array.
let array = [1, 2, 3];
array.push(4);
// array is now [1, 2, 3, 4]

2. splice():

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let array = [1, 2, 3, 4, 5];
array.splice(2, 1); // Removes 1 element at index 2
// array is now [1, 2, 4, 5]

3. concat():

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
let array1 = [1, 2];
let array2 = [3, 4];
let newArray = array1.concat(array2);
// newArray is [1, 2, 3, 4]

4. forEach():

The forEach() method executes a provided function once for each array element.
let array = [1, 2, 3];
array.forEach(function(element) {
console.log(element);
});
// Output: 1 2 3

5. map():

The map() method creates a new array with the results of calling a provided function on every element in the calling array.
let array = [1, 2, 3];
let doubledArray = array.map(function(element) {
return element * 2;
});
// doubledArray is [2, 4, 6]

6. filter():

The filter() method creates a new array with all elements that pass the test implemented by the provided function.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Example 1: Filtering even numbers
let evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
// evenNumbers is [2, 4, 6, 8, 10]
// Example 2: Filtering numbers greater than 5
let greaterThanFive = numbers.filter(function(number) {
return number > 5;
});
// greaterThanFive is [6, 7, 8, 9, 10]

JAVASCRIPT ARRAY METHODS
CODING

7. find():

The find() method returns the first element in the array that satisfies the provided testing function.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Find the first even number
let firstEvenNumber = numbers.find(number => number % 2 === 0);
// firstEvenNumber is 2
// Find the first number greater than 5
let firstGreaterThanFive = numbers.find(number => number > 5);
// firstGreaterThanFive is 6

8. sort():

The sort() method sorts the elements of an array in place and returns the sorted array.
let numbers = [4, 2, 5, 1, 3];
numbers.sort(); // Sorts the array as strings: [1, 2, 3, 4, 5]
// To sort numbers numerically
numbers.sort((a, b) => a – b); // Sorts the array numerically: [1, 2, 3, 4, 5]

9. indexOf():

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
let fruits = [‘apple’, ‘banana’, ‘orange’, ‘pear’];
let indexBanana = fruits.indexOf(‘banana’);
// indexBanana is 1
let indexGrape = fruits.indexOf(‘grape’);
// indexGrape is -1, as ‘grape’ is not in the array

10. includes():

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
let fruits = [‘apple’, ‘banana’, ‘orange’, ‘pear’];
let hasBanana = fruits.includes(‘banana’);
// hasBanana is true
let hasGrape = fruits.includes(‘grape’);
// hasGrape is false, as ‘grape’ is not in the array

 

NOTE:

  • Thank you for taking the time to read this post. I hope you found it insightful and informative.  If you enjoyed this post, here are some additional resources you might find helpful:
  1. JavaTpoint
  2. Geeks for Geeks
  3. W3School
  4. Javascript.info
  5. Programiz
  6. MDN Documentation
  7. chatgpt

Feel free to share your thoughts or questions in the comments below. I love hearing from my readers!

 Stay curious and keep learning,

 [Decode Now]

 

Leave a Comment

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