The Array object provides a variety of methods. Let’s take a look at some of the most useful ones.
Methods to Access or Modify Elements
| Method | Description |
|---|---|
arr[index] | Accesses a specific element by its index. |
at(index) | Accesses an element by specifying the index |
The arr[index] method accesses a specific element in an array using its index.
let arr = [1, 2, 3, 4];
console.log(arr[0]); // 1
console.log(arr[2]); // 3The at(index) method allows accessing an array element, supporting negative indices that reference elements from the end.
let arr = [1, 2, 3, 4];
console.log(arr.at(1)); // 2
console.log(arr.at(-1)); // 4Methods to Modify Elements
| Method | Description |
|---|---|
push() | Adds one or more elements to the end of the array. |
pop() | Removes the last element from the array. |
shift() | Removes the first element from the array. |
unshift() | Adds one or more elements to the beginning of the array. |
splice() | Removes, replaces, or adds elements. |
The push() method adds one or more elements to the end of the array.
let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // [1, 2, 3, 4]
arr.push(5, 6);
console.log(arr); // [1, 2, 3, 4, 5, 6]The pop() method removes the last element from an array and returns it.
let arr = [1, 2, 3];
console.log(arr.pop()); // 3
console.log(arr); // [1, 2]The shift() method removes the first element from the array and returns it.
let arr = [1, 2, 3];
console.log(arr.shift()); // 1
console.log(arr); // [2, 3]The unshift() method adds one or more elements to the beginning of the array.
let arr = [2, 3];
arr.unshift(1);
console.log(arr); // [1, 2, 3]
arr.unshift(-1, 0);
console.log(arr); // [-1, 0, 1, 2, 3]The splice() method modifies the array by removing or replacing elements.
let arr = [1, 2, 3, 4];
arr.splice(2, 1, 10); // Removes 1 element at index 2 and adds 10
console.log(arr); // [1, 2, 10, 4]Methods to Modify Order
| Method | Description |
|---|---|
sort() | Sorts the elements of the array according to a comparison function. |
reverse() | Reverses the order of the elements in the array. |
copyWithin() | Copies a part of the array within the same array. |
The sort() method sorts the elements of an array according to a comparison function.
let arr = [4, 1, 3, 2];
arr.sort((a, b) => a - b); // Ascending order
console.log(arr); // [1, 2, 3, 4]The reverse() method reverses the order of the elements in an array.
let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // [3, 2, 1]The copyWithin() method copies a section of the array into itself.
let arr = [1, 2, 3, 4, 5];
arr.copyWithin(2, 0, 2); // Copies the range arr[0, 1] starting at index 2
console.log(arr); // [1, 2, 1, 2, 5]Search and Check Methods
| Method | Description |
|---|---|
includes() | Checks if an array contains an element. |
indexOf() | Returns the first index of an element or -1 if not found. |
lastIndexOf() | Returns the last index of an element or -1 if not found. |
find() | Returns the first element that meets the condition. |
findIndex() | Returns the index of the first element that meets the condition. |
The includes() method checks if an array contains a specific element.
let arr = [1, 2, 3, 4];
console.log(arr.includes(3)); // true
console.log(arr.includes(5)); // falseThe indexOf() method returns the first index at which an element is found in the array, or -1 if not found.
let arr = [1, 2, 3, 4];
console.log(arr.indexOf(3)); // 2
console.log(arr.indexOf(5)); // -1The lastIndexOf() method returns the last index at which an element is found in the array, or -1 if not found.
let arr = [1, 2, 3, 2, 4];
console.log(arr.lastIndexOf(2)); // 3
console.log(arr.lastIndexOf(5)); // -1The find() method returns the first element in an array that meets a specific condition defined by a testing function. If no elements meet the condition, it returns undefined.
let arr = [5, 12, 8, 130, 44];
let found = arr.find((element) => element > 10);
console.log(found); // 12
let notFound = arr.find((element) => element > 200);
console.log(notFound); // undefinedThe findIndex() method returns the index of the first element that meets a specific condition defined by a testing function. If no elements meet the condition, it returns -1.
let arr = [5, 12, 8, 130, 44];
let index = arr.findIndex((element) => element > 10);
console.log(index); // 1 (the index of the value 12)
let notFoundIndex = arr.findIndex((element) => element > 200);
console.log(notFoundIndex); // -1Transformation Methods
| Method | Description |
|---|---|
slice() | Creates a new array from a copy of a part of the original. |
concat() | Joins two or more arrays and returns a new one. |
flat() | Flattens an array of arrays into a single array. |
flatMap() | Similar to map(), but with flattened results. |
join() | Joins all elements of the array into a string. |
fill() | Fills all elements of the array from a start position to an end with a value. |
The slice() method creates a shallow copy of a section of an array.
let arr = [1, 2, 3, 4];
let newArr = arr.slice(1, 3);
console.log(newArr); // [2, 3]The concat() method joins two or more arrays and returns a new one.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // [1, 2, 3, 4]The flat() method flattens an array of arrays into a single array.
let arr = [1, [2, 3], [4, 5]];
let flattened = arr.flat();
console.log(flattened); // [1, 2, 3, 4, 5]The flatMap() method first maps each element with a function and then flattens the result.
let arr = [1, 2, 3];
let newArr = arr.flatMap((x) => [x, x * 2]);
console.log(newArr); // [1, 2, 2, 4, 3, 6]The join() method joins all elements of an array into a string, separating them with the value specified as an argument.
let arr = ['a', 'b', 'c', 'd'];
let joined = arr.join('-');
console.log(joined); // 'a-b-c-d'
let defaultJoin = arr.join();
console.log(defaultJoin); // 'a,b,c,d'The fill() method changes all elements in a specific range of the array to a static value.
let arr = [1, 2, 3, 4, 5];
arr.fill(0, 2, 4); // Fills indices 2 to 4 with 0
console.log(arr); // [1, 2, 0, 0, 5]
arr.fill(9); // Fills the entire array with 9
console.log(arr); // [9, 9, 9, 9, 9]Check Methods
| Method | Description |
|---|---|
Array.isArray() | Checks if a value is an array. |
The Array.isArray() method checks if the passed value is an array.
let arr = [1, 2, 3];
console.log(Array.isArray(arr)); // true
console.log(Array.isArray("not an array")); // false