It was a great many years ago ECMAScript 5 came out to javascript now, and brought not only lots of exciting news, but also a great deal of new array methods. Methods that does everything from looping through, changing, filtering and extracting values from them.

Below is a list over all the array methods that were included in ECMAScript 5. The browser support is great. All modern browsers support them, and they work in internet explorer all the way down to version 9. In node they can be used in all versions released after 2011.

ForEach

var arr = ['apple', 'pear', 'peach'];

arr.forEach(function (fruit, index, array) {
console.log(fruit);
});

// apple, pear and peach is printed out

Foreach is a method for looping through arrays in javascript instead of having to use the for or while loop. It takes a function as argument (doesn't have to be anonymous) and executes that function for every value in the array. It's not necessary to set the parameters index and array if you don't need them, they're optional. In many cases it's enough with just the first argument which is the actual value being iterated.

Every

var arr = [1, 2, 3];

var res = arr.every(function (number) {
return number > 0;
});

console.log(res); // true since all values are greater than 0

Every loops through an array just like forEach does, except you give it a condition that will be tested for every value in the array. If the condition is true for all values in the array the boolean true is returned, otherwise false.

Some

var arr = [-20, 0, 5];

var res = arr.some(function (number) {
return number > 0;
});

console.log(res); // true since at least one number is greater than 0

Some does the same thing as every, except it's enough that the condition results in true for just one value for the function to return true. If no value fulfills the condition false is returned.

Map

var arr = ['apple', 'pear', 'peach'];

var newArr = arr.map(function (fruit) {
return fruit + ' is nice';
});

console.log(newArr); // ['apple is nice', 'pear is nice', 'peach is nice']

Map does a change to all values in the array. It doesn't modify the current array directly, instead it always returns a new array with the modified values, that can be stored in a new variable for example

Filter

var arr = [1, 2, 3, 4, 5];

var newArr = arr.filter(function (number) {
return number > 2;
});

console.log(newArr); // [3, 4, 5]

Filter in javascript loops through an array and returns a new just like map does. But it doesn't change any values, instead it "filters out values" and only includes the values in the new array that its condition results in true for.

IndexOf

var arr = ['apple', 'pear', 'peach'];

arr.indexOf('apple'); // 0
arr.indexOf('peach'); // 2
arr.indexOf('banana'); // -1

arr.push('pear'); // adds the value "pear" last in the arrayen
arr.lastIndexOf('pear'); // 3

IndexOf returns a values position in an array from the start, starting from position 0. If the value doesn't exist -1 is returned. There's also another method called lastIndexOf, which does the same thing as indexOf except it returns the values position from the end instead.

Reduce

var arr = [1, 2, 3];

var res = arr.reduce(function (sum, number) {
return sum + number;
});

console.log(res); // 6 (1 + 2 + 3 = 6)

res = arr.reduceRight(function (sum, number) {
return sum - number;
});

console.log(res); // 0 (3 - 2 - 1 = 0)

Reduce reduces an arrays values into a single value. Depending on what argument it's given it returns a sum of all the numbers in the array from first to last. The first parameter sum is a placeholder for the sum, and the initial value. The second parameter is the current number being iterated.

There's also a method called reduceRight which does the same thing as reduce, except it starts from the end (last value) instead.