Top 6 JavaScript Array Methods with Examples

Hi learners, welcome to DesignWithRehana. In this article I am going to share Top 6 JavaScript Array Methods with examples and PDF. I hope these JavaScript array methods cheat-sheet will be helpful for you.

1. forEach()

This method is used to call a function for each array element. However, this method will not be executed on empty elements. This method iterates through the items of an array, executing the input function for each element.

Code Example

const givenArray = [1, 2, 3, 4, 5, 6];
givenArray.forEach(function (currentValue) {
    console.log(currentValue);
});
// Expected Output: 1 2 3 4 5 6

2. sort()

sort() JavaScript array method is used to sort the given array. It compares two values and returns the same array as the reference which is now a sorted array.

Code Example

const givenArray = [40, 20, 50, 23, 3, 12];
givenArray .sort()
console.log(givenArray );
// Expected Output: [12, 20, 23, 3, 40, 50]

3. join()

This javascript array method is used to merge all of the array’s items into a string. It also uses a comma (,) to divide the array elements. It won’t change the original array and will not generate a new array. It returns the specified array as a string, that is split by a comma (,) by default.

Code Example

const myEbooks = [‘HTML‘, ‘CSS‘, ‘JavaScript‘, ‘Bootstrap‘, ‘ReactJS];

const myEbooksString = myEbooks.join()

console.log(myEbooksString);

//Expected Output: true: HTML,CSS,JavaScript,Bootstrap,ReactJS

4. reverse()

This array function reverses the given array. It returns the same array, but the first element becomes the last and the last element becomes the first. Similarly, additional array elements turned in the other direction.

Code Example

const myEbooks = [‘HTML‘, ‘CSS‘, ‘JavaScript‘, ‘Bootstrap‘, ‘ReactJS];
const myEbooksString = myEbooks.reverse()

console.log(myEbooksString);

//Expected Output: true: ReactJS, Bootstrap, JavaScript, CSS, HTML

5. reduce()

This array method is used to perform operations on an array in order to get a single value. It is an iterative process.

Code Example

const myValues= [2, 3, 4, 3, 2, 6, 9, 4, 5]
let sumOfValues = myValues.reduce(function (previousValue, currentValue) {
    return previousValue + currentValue;
}, 0)
console.log(sumOfValues);

 

// Expected Output:  38

6. map()

This javascript array method iterates across an array. It calls the method for each array element. And for each array element, it produces a new array by invoking a certain procedure.

Code Example

const myEbooks= [‘HTML’, ‘CSS’, ‘JavaScript’, ‘Bootstrap’];
myEbooks.forEach(function (currentValue) {
    console.log(currentValue);
});

// Expected Output:

/*
0 HTML
1 CSS
2 JavaScript
3 Bootstrap
*/

HTML Input Types Cheatsheet PDF

Leave a Comment