JavaScript Array Functions

Md. Abdullah Al Mahmud Khan
Oceanize Lab Geeks
Published in
2 min readMay 6, 2019

In this post i will describe different type of array functions. You should learn all array method to improve your JavaScript skill. I will try to make easy way to learn different type of array method.

forEach()

The forEach() method calls a provided function once for each element in an array, in order. forEach() does not execute the function for array elements without values.

Syntax
array.forEach(function(currentValue, index, arr))

Example:
var array = [1,2,3,4,5];
array.forEach(function(currentValue, index, arr){
console.log(“Current value: “+ currentValue + “ Index: “ + index + “Array: “ + arr);
})

filter()

The filter() method creates an array filled with all array elements that pass a test (provided as a function)

Syntax
array.filter(function(currentValue, index, arr))

Example:
const array = [32, 33, 16, 40];
const result = array.filter(function(age) {
return age >= 18;
})

console.log(result); // output will be [ 32, 33, 40 ]

map()

The map() method creates a new array with the results of calling a function for every array element

Syntax
array.map(function(currentValue, index, arr))

Example:
const array = [1,2,3,4,5];
const newArray = array.map(function(value){
return value + 2;
})

console.log(newArray); //Output will be [ 3, 4, 5, 6, 7 ]

sort()

This method used to arrange/sort array’s item either ascending or descending order

const arr = [1, 2, 3, 4, 5, 6];
const alpha = [‘e’, ‘a’, ‘c’, ‘u’, ‘y’];

// sort in descending order
descOrder = arr.sort((a, b) => a > b ? -1 : 1);
console.log(descOrder); // output: [6, 5, 4, 3, 2, 1]

// sort in ascending order
ascOrder = alpha.sort((a, b) => a > b ? 1 : -1);
console.log(ascOrder); // output: [‘a’, ‘c’, ‘e’, ‘u’, ‘y’]

reduce()

The reduce() method reduces the array to a single value.
The reduce() method executes a provided function for each value of the array (from left-to-right). The return value of the function is stored in an accumulator (result/total).

Syntax
array.reduce(function(total, currentValue, currentIndex, arr))

Example
const array = [1,2,3,4,5];
const newArray = array.reduce(function(a,b){
return a + b;
})
console.log(newArray); // Output will be 15

some()

This method check if at least one of array’s item passed the condition. If passed, it return ‘true’ otherwise ‘false’.

Syntax
array.some(function(currentValue, index, arr))

Example
const array = [1,2,3,4,5];
const newArray = array.some(function(num){
return num > 3;
})

console.log(newArray); // Output will be true

every()

This method check if all array’s item passed the condition. If passed, it return ‘true’ otherwise ‘false’.

Syntax
array.every(function(currentValue, index, arr))

const array = [1,2,3,4,5];
const newArray = array.every(function(num){
return num > 3;
})

console.log(newArray); // Output will be false

concat()

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

Syntax
array1.concat(array2, array3, …, arrayX)

Example
var hege = [“Cecilie”, “Lone”];
var stale = [“Emil”, “Tobias”];
var children = hege.concat(stale);

console.log(children); // Output will be [ ‘Cecilie’, ‘Lone’, ‘Emil’, ‘Tobias’]

Thank you very much for reading my post.

--

--