JavaScript Array Methods

Array.from()
: static method create new , shallow-copied Array instance from an iterable object.console.log(Array.from("wasif"); // Expected Output: ["w" , "a" , "s", "i" , "f"]
Array.isArray()
: static method determines whether the passed value is anArray
.const arr = [1,2,3] console.log(Array.isArray(arr)) //expected output : true
Array.of()
: static method creates a newArray
instance from a variable number of arguments, regardless of number or type of the arguments.console.log(Array.of("foo", 2, "bar", true)); // Expected output: Array ["foo", 2, "bar", true] console.log(Array.of()); // Expected output: Array []
at()
: This method take an integer value and return the item at that index , if negative integer it count from the last element of the array.const arr = [1,2,45,6,8] console.log(arr.at(3)); // ans : 6 console.log(arr.at(-1)) // 8
every() :
Theevery()
method ofArray
instances tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.some() :
Thesome()
method ofArray
instances tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array```javascript
const IsGreater = (currentValue) =>currentValue > 20; const arr = [21, 34,56,78]; console.log(arr.every(IsGreater)); //true
const arr1 = [1,2,3,44,6]; console.log(arr1.some(IsGreater)); // true
6. `fill()` : This Changes all the element within the range of indices in an array. It return the modifies array .
```javascript
const arr = [1,2,3,4];
console.log(arr.fill(0 , 1, 3))
console.log(arr) //[ 1, 0, 0, 4 ]
filter()
: create a shallow copy of a portion of a given array , which passes the given condition.const arr = [1,2,3,4,5] const ans = arr.filter((n) => n > 2) console.log(ans); // [3,4,5] console.log(arr) // [1,2,3,4,5]
find() :
Thefind()
method ofArray
instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function,undefined
is returned.findLast() :
ThefindLast()
method ofArray
instances iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function,undefined
is returned.findIndex() :
ThefindIndex()
method ofArray
instances returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.findLastIndex()
: ThefindLastIndex()
method ofArray
instances iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.indexOf() :
TheindexOf()
method ofArray
instances returns the first index at which a given element can be found in the array, or -1 if it is not present.includes() :
Theincludes()
method ofArray
instances determines whether an array includes a certain value among its entries, returningtrue
orfalse
as appropriate.```javascript
const array = [5, 12, 8, 130, 44]; const found_first = array.find((element) => element > 10);
console.log(found_first); // Expected output: 12
const last_found = array.findLast((element) => element>10); console.log(last_found) // Expected Output : 44
console.log(array.findIndex(found_first)) //ouput : Index of 12 : 1
console.log(array.findLastIndex(last_found) // output : 4
console.log(array.indexOf(130)) //expected output : 3
console.log(array.includes(12)) // output:true
9. `forEach()` : The `forEach()` method of [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) [insta](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)nces executes a provided function once for each array element.
```javascript
const arr = [1, 2, 3, 4];
arr.forEach((ele)=>console.log(ele*2))
//output:
//2
//4
//6
//8
join() :
Thejoin()
method ofArray
instances creates and returns a new string by concatenating all of the elements in this array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.const elements = ["Fire", "Air", "Water"]; console.log(elements.join()); // Expected output: "Fire,Air,Water" console.log(elements.join("")); // Expected output: "FireAirWater" console.log(elements.join("-")); // Expected output: "Fire-Air-Water"
map() :
Themap()
method ofArray
instances creates a new array populated with the results of calling a provided function on every element in the calling array.const arr = [12,2,2]; const arr1 = arr.map((element)=>element*2) console.log(arr1); //[ 24, 4, 4 ] console.log(arr); //[ 12, 2, 2 ]
reduce()
: Thereduce()
method ofArray
instances executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).
//Syntax //reduce(callbackFn, initialValue) const array = [1, 2, 3, 4]; // 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array.reduce( (accumulator, currentValue) => accumulator + currentValue, initialValue, ); console.log(sumWithInitial); // Expected output: 10
reverse() :
Thereverse()
method ofArray
instances reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.toReversed() :
ThetoReversed()
method ofArray
instances is the copying counterpart of thereverse()
method. It returns a new array with the elements in reversed order.const arr = [12, 2, 2]; console.log(arr); // [ 12, 2, 2 ] console.log(arr.reverse()) // [2,2,12] console.log(arr) // [2,2,12] const newArray = arr.toReversed(); console.log(newArray); //[ 12, 2, 2 ] console.log(arr) //[ 2, 2, 12 ]
sort() :
Thesort()
method ofArray
instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
toSorted() :
ThetoSorted()
method ofArray
instances is the copying version of thesort()
method. It returns a new array with the elements sorted in ascending order.const months = ["March", "Jan", "Feb", "Dec"]; months.sort(); console.log(months); // Expected output: Array ["Dec", "Feb", "Jan", "March"] const array = [1, 30, 4, 21, 100000]; array.sort(); console.log(array); // Expected output: Array [1, 100000, 21, 30, 4] //toSorted() const arr = [1,5,4,2,3] const newArray = array.toSorted(); console.log(newArray) // [1,2,3,4,5] console.log(arr) //[1,5,4,2,3]
Subscribe to my newsletter
Read articles from Syed Wasif Hussain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
