Javascript Array - 30 Methods and 1 Property (Reference)
Array in Javascript is a data structure that can hold a collection of values. It is used to store multiple values in a single variable. The values are accessed by an index number.
Array Methods:
Array Properties:
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.
var arr1 = ["aaa", "bbb"];
var arr2 = ["ccc", "ddd"];
var x = arr1.concat(arr2);
// ["aaa","bbb","ccc","ddd"]
var arr1 = ["aaa", "bbb"];
var arr2 = ["ccc", "ddd"];
var arr3 = ["eee", "fff"];
var x = arr1.concat(arr2, arr3);
// ["aaa","bbb","ccc","ddd","eee","fff"]
// arr1.concat(arr2, arr3, ..., arrX)
join()
The join() method is used to join all elements of an array into a string.
var arr = ["aaa", "bbb"];
var x = arr.join();
// aaa,bbb
var arr = ["aaa", "bbb"];
var x = arr.join("-");
// aaa-bbb
// arr.join(separator)
entries()
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
var arr = ["aaa", "bbb"];
var x = arr.entries();
for (item of x) {
// 0,aaa
// 1,bbb
}
keys()
The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
var arr = ["aaa", "bbb"];
var x = arr.keys();
for (item of x) {
// 0
// 1
}
every()
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
return val >= 3;
}
var x = arr.every(TEST);
// false
// arr.every(Function, thisValue)
some()
The some() method tests whether some element in the array passes the test implemented by the provided function. It returns a Boolean value.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
return val >= 3;
}
var x = arr.some(TEST);
// true
// arr.some(Function, thisValue)
filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
return val >= 3;
}
var x = arr.filter(TEST);
// [3,4,5,6]
// arr.filter(Function, thisValue)
map()
The map() method creates a new array with the results of calling a provided function on every element in this array.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
return val * 2;
}
var x = arr.map(TEST);
// [2,4,6,8,10,12]
// arr.map(Function, thisValue)
forEach()
The forEach() method executes a provided function once for each array element.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
// Code
}
var x = arr.forEach(TEST);
// arr.forEach(Function, thisValue)
find()
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. Otherwise undefined is returned.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(val, index, arr) {
return val >= 3;
}
var x = arr.find(TEST);
// 3
// arr.find(Function, thisValue)
findIndex()
The findIndex() method returns the index of the first element in the provided array that satisfies the provided testing function. Otherwise -1 is returned.
var arr = ["1,2,3,4,5,6"];
function TEST(val, index, arr) {
return val >= 3;
}
var x = arr.findIndex(TEST);
// 2 - index value
// arr.findIndex(Function, thisValue)
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.
var arr = ["aaa", "bbb", "aaa", "bbb"];
var x = arr.indexOf("bbb");
// 1
var arr = ["aaa", "bbb", "aaa", "bbb"];
var x = arr.indexOf("ccc");
// -1
// arr.indexOf(item, start)
lastIndexOf()
The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
var arr = ["aaa", "bbb", "aaa", "bbb"];
var x = arr.lastIndexOf("bbb");
// 3
var arr = ["aaa", "bbb", "aaa", "bbb"];
var x = arr.lastIndexOf("ccc");
// -1
// arr.lastIndexOf(item, start)
includes()
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
var arr = ["aaa", "bbb"];
var x = arr.includes("bbb");
// true
// arr.includes(element, start)
push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
var arr = ["aaa", "bbb"];
arr.push("ccc");
// ["aaa","bbb","ccc"]
// arr.push(item1, item2, ..., itemX)
unshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
var arr = ["aaa", "bbb"];
arr.push("ccc");
// ["ccc","aaa","bbb"]
// arr.unshift(item1, item2, ..., itemX)
pop()
The pop() method removes the last element from an array and returns that element.
var arr = ["aaa", "bbb"];
arr.pop();
// ["aaa"]
shift()
The shift() method removes the first element from an array and returns that element.
var arr = ["aaa", "bbb"];
arr.shift();
// ["bbb"]
reduce()
The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(total, val, index, arr) {
return total + val;
}
var x = arr.reduce(TEST);
// 21
// arr.reduce(Function, initial_value)
reduceRight()
The reduceRight() method applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value.
var arr = [1, 2, 3, 4, 5, 6];
function TEST(total, val, index, arr) {
return total + val;
}
var x = arr.reduceRight(TEST);
// 21
// arr.reduceRight(Function, initial_value)
reverse()
The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.
var arr = ["aaa", "bbb"];
arr.reverse();
// ["bbb","aaa"]
sort()
The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.
var arr = ["aaa", "bbb", "ddd", "ccc"];
arr.sort();
// ["aaa","bbb","ccc","ddd"]
var arr = [40, 100, 1, 5, 25, 10];
arr.sort(TEST);
function TEST(a, b) {
return a - b;
}
// [1,5,10,25,40,100]
// arr.sort(compareFunction)
slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
var arr = ["aaa", "bbb", "ccc", "ddd"];
var x = arr.slice(1, 3);
// ["bbb","ccc"]
// arr.slice(start, end)
splice()
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
var arr = ["aaa", "bbb", "ccc", "ddd"];
var x = arr.splice(2, 0, "xxx", "yyy");
// ["aaa","bbb","xxx","yyy","ccc","ddd"]
// arr.splice(index, howmany_to_remove, item1, ....., itemX)
copyWithin()
The copyWithin() method copies array elements within the array to another position, overwriting existing elements.
var arr = ["aaa", "bbb", "ccc", "ddd"];
var x = arr.copyWithin(2, 0);
// ["aaa","bbb","aaa","bbb"]
var arr = ["aaa", "bbb", "ccc", "ddd"];
var x = arr.copyWithin(2, 1, 2);
// ["aaa","bbb","aaa","ddd"]
// arr.copyWithin(target, start, end)
fill()
The fill() method fills all the elements of an array from a start index to an end index with a static value.
var arr = ["aaa", "bbb"];
arr.fill("xxx");
// ["xxx","xxx"]
var arr = ["aaa", "bbb", "ccc", "ddd"];
arr.fill("xxx", 1, 2);
// ["aaa","xxx","ccc","ddd"]
// array.fill(value, start, end)
toString()
The toString() method returns a string representing the specified array and its elements.
var arr = ["aaa", "bbb"];
var x = arr.toString();
// ["aaa","bbb"]
valueOf()
The valueOf() method returns the primitive value of the specified object.
var arr = ["aaa", "bbb"];
var x = arr.valueOf();
// ["aaa","bbb"]
from()
The from() method creates a new array instance from an array-like or iterable object.
var txt = "ABCDEFG";
var x = Array.from(txt);
// ["A","B","C","D","E","F","G"]
var txt = "ABCDEFG";
var x = Array.from(txt, TEST);
function TEST(val, index) {
return val.toLowerCase();
}
// ["a","b","c","d","e","f","g"]
// Array.from(object, mapFunction, thisValue)
isArray()
The isArray() method determines whether the passed value is an Array.
Array.isArray([1, 2, 3]); // true
Array.isArray({ foo: 123 }); // false
Array.isArray("foobar"); // false
Array.isArray(undefined); // false
length
The length property represents the number of elements in an array.
var array = ["one", "two"];
var x = array.length;
// x = 2
