# Array Reference
This page is a collection of all the Array methods. Don't try to absorb these all at once. Come back to them every week to learn more.
One thing to pay special attention to with Array methods is whether the method changes the original Array or whether it creates a new one.
# Sorting Arrays
At some point you will want to sort an Array. Thankfully, the JavaScript array object comes with a built-in sort
method. The sort
method will change the original Array.
The sort will sort the items in the Array in alphabetical order.
let names = ['Bob', 'Linda', 'Tina', 'Louise', 'Gene'];
names.sort();
console.log(names); //the sorted version will be logged
2
3
if you want to sort the Array in numerical order or in some other custom way, you can pass a function to the method which will be used to sort the values. Watch this video to learn more.
# Array Methods
# Array.concat( )
The Array concat method allows you to add another Array on to the end of the original one. It returns a new Array.
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let arr3 = arr1.concat(arr2); // [1,2,3,4,5,6]
2
3
# Array.includes( )
The Array includes method will loop through an Array looking for a match for the value that you provide. The value you provide can be any Primitive. Optionally you can add a starting position to begin looping. It returns a true
or false
value.
let arr1 = ['bob', 'mary', 'fred', 'alice'];
let arr2 = ['Bob', 'Mary', 'Fred', 'Alice'];
arr1.includes('Fred'); //false
arr2.includes('Fred'); //true
2
3
4
# Array.some( )
The Array some method will loop through the values in an Array and returns true
as soon as it finds the first value that meets your condition. It accepts a function as it's parameter. This makes it very efficient because it could exit before looping through all values.
let arr2 = ['Bob', 'Mary', 'Fred', 'Alice'];
arr2.some((name) => {
return name == 'Fred';
}); //true
2
3
4
# Array.every( )
The Array every method will check every value in an Array to see if they all meet a condition that you choose. It returns a true
or false
.
let nums = [1, 3, 5, 7, 9, 15, 23];
//check if every number is odd
nums.every((num) => {
if (num % 2 == 1) {
return true;
} else {
return false;
}
});
2
3
4
5
6
7
8
9
# Array.join( )
The Array join method will convert all the values in the Array into Strings and then combine all the Strings into a single String. You have the option of putting a separator between each of the String values as their are joined.
let words = [
'I',
'love',
'the',
'smell',
'of',
'napalm',
'in',
'the',
'morning',
];
words.join(' '); // "I love the smell of napalm in the morning"
2
3
4
5
6
7
8
9
10
11
12
# Array.filter( )
The Array filter method will loop through the Array values and, based on your criteria, return a new filtered version of the original Array.
let nums = [4, 2, 8, 3, 5, 7, 10];
//create a new array with just the even numbers
let evens = nums.filter((num) => {
if (num % 2 == 0) {
return true;
} else {
return false;
}
}); // [4, 2, 8, 10]
2
3
4
5
6
7
8
9
# Array.reduce( )
The Array reduce method will loop through an Array and return a single value. You provide a function and a starting value for comparison. The starting value is often called the accumulator.
//find the biggest number in an array that is positive
let nums = [-23, 45, -3, 0, 144, -42, 87];
let big = nums.reduce((acc, num) => {
if (num > 0 && num > acc) {
return num;
} else {
return acc;
}
}, 0);
2
3
4
5
6
7
8
9