JavaScript Array Methods and Properties
date
May 9, 2024
slug
js-array-methods
status
Published
tags
js
javascript
summary
Here's a brief description and example for each of the listed array methods
type
Post
- new Array: Creates a new Array.
let newArray = new Array();
- at(): Returns an indexed element of an array.
const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.at(1)); // Output: banana
- concat(): Joins arrays and returns an array with the joined arrays.
const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const newArray = array1.concat(array2);
- constructor: Returns the function that created the Array prototype.
const arr = []; console.log(arr.constructor === Array); // Output: true
- copyWithin(): Copies array elements within the array, to and from specified positions.
const array = ['a', 'b', 'c', 'd', 'e']; array.copyWithin(0, 3, 4); // Changes array to ['d', 'b', 'c', 'd', 'e']
- entries(): Returns a key/value pair Array Iteration Object.
const fruits = ['apple', 'banana', 'cherry']; const iterator = fruits.entries();
- every(): Checks if every element in an array passes a test.
const ages = [32, 33, 16, 40]; const isAdult = (age) => age >= 18; console.log(ages.every(isAdult)); // Output: false
- fill(): Fills the elements in an array with a static value.
const array = [1, 2, 3, 4]; array.fill(0, 2, 4); // Changes array to [1, 2, 0, 0]
- filter(): Creates a new array with every element in an array that passes a test.
const numbers = [10, 20, 30, 40, 50]; const result = numbers.filter(number => number > 30);
- find(): Returns the value of the first element in an array that passes a test.
const array = [5, 12, 8, 130, 44]; const found = array.find(element => element > 10);
- findIndex(): Returns the index of the first element in an array that passes a test.
const array = [5, 12, 8, 130, 44]; const index = array.findIndex(element => element > 10);
- findLast(): Returns the value of the last element in an array that passes a test.
const array = [5, 12, 8, 130, 44]; const found = array.findLast(element => element > 10);
- findLastIndex(): Returns the index of the last element in an array that passes a test.
const array = [5, 12, 8, 130, 44]; const index = array.findLastIndex(element => element > 10);
- flat(): Concatenates sub-array elements.
const arr = [1, 2, [3, 4]]; const flatArr = arr.flat();
- flatMap(): Maps all array elements and creates a new flat array.
const arr = [1, 2, 3]; const mapped = arr.flatMap(x => [x * 2]);
- forEach(): Calls a function for each array element.
const array = [1, 2, 3]; array.forEach(element => console.log(element));
- from(): Creates an array from an object.
const arrayLike = { 0: 'a', 1: 'b', 2: 'c', length: 3 }; const array = Array.from(arrayLike);
- includes(): Checks if an array contains the specified element.
const array = [1, 2, 3]; const isIncluded = array.includes(2);
- indexOf(): Searches the array for an element and returns its position.
const array = [2, 9, 9]; const index = array.indexOf(9);
- isArray(): Checks whether an object is an array.
const array = [1, 2, 3]; const isArray = Array.isArray(array);
- join(): Joins all elements of an array into a string.
const array = ['Hello', 'world!']; const joined = array.join(' ');
- keys(): Returns an Array Iteration Object containing the keys of the original array.
const array = ['a', 'b', 'c']; const iterator = array.keys();
- lastIndexOf(): Searches the array for an element, starting at the end, and returns its position.
const array = [2, 5, 9, 2]; const index = array.lastIndexOf(2);
- length: Sets or returns the number of elements in an array.
const array = [1, 2, 3]; const length = array.length;
- map(): Creates a new array with the result of calling a function for each array element.
const numbers = [1, 4, 9]; const roots = numbers.map(Math.sqrt);
- of(): Creates an array from a number of arguments.
const array = Array.of(1, 2, 3);
- pop(): Removes the last element of an array, and returns that element.
const array = [1, 2, 3]; const lastElement = array.pop();
- prototype: Allows you to add properties and methods to an Array object.
Array.prototype.myCustomFunction = function() { // Custom function logic };
- push(): Adds new elements to the end of an array, and returns the new length.
const array = [1, 2, 3]; const newLength = array.push(4, 5);
- reduce(): Reduces the values of an array to a single value (going left-to-right).
const array = [1, 2, 3, 4]; const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue);
- reduceRight(): Reduces the values of an array to a single value (going right-to-left).
const array = [1, 2, 3, 4]; const sum = array.reduceRight((accumulator, currentValue) => accumulator + currentValue);
- reverse(): Reverses the order of the elements in an array.
const array = ['one', 'two', 'three']; array.reverse();
- shift(): Removes the first element of an array, and returns that element.
const array = [1, 2, 3]; const firstElement = array.shift();
- slice(): Selects a part of an array, and returns the new array.
const array = [1, 2, 3, 4, 5]; const sliced = array.slice(2, 4);
- some(): Checks if any of the elements in an array pass a test.
const array = [1, 2, 3, 4, 5]; const hasEvenNumber = array.some(element => element % 2 === 0);
- sort(): Sorts the elements of an array.
const array = [2, 1, 3]; array.sort();
- splice(): Adds or removes array elements.
const array = ['a', 'b', 'c', 'd']; array.splice(2, 0, 'x', 'y');
- toReversed(): Reverses the order of array elements (to a new array).
const array = [1, 2, 3]; const reversedArray = array.slice().reverse();
- toSorted(): Sorts the elements of an array (to a new array).
const array = [3, 2, 1]; const sortedArray = array.slice().sort();
- toSpliced(): Adds or removes array elements (to a new array).
const array = ['a', 'b', 'c']; const newArray = array.slice().splice(1, 1);
- toString(): Converts an array to a string, and returns the result.
const array = ['a', 'b', 'c']; const string = array.toString();
- unshift(): Adds new elements to the beginning of an array, and returns the new length.
const array = [1, 2, 3]; const newLength = array.unshift(0);
- valueOf(): Returns the primitive value of an array.
const array = [1, 2, 3]; const primitiveValue = array.valueOf();
- with(): Returns a new array with updated elements. (Note: There is no
with()
method for arrays in JavaScript.)