JavaScript Array Methods

  1. 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"]
    
  2. Array.isArray(): static method determines whether the passed value is an Array.

     const arr = [1,2,3]
     console.log(Array.isArray(arr)) 
     //expected output : true
    
  3. Array.of() : static method creates a new Array 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 []
    
  4. 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
    
  5. every() : The every() method of Array instances tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

    some() : The some() method of Array 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 ]
  1. 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]
    
  2. find() : The find() method of Array 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() : The findLast() method of Array 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() : The findIndex() method of Array 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() : The findLastIndex() method of Array 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() : The indexOf() method of Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.

    includes() : The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false 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
  1. join() : The join() method of Array 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"
    
  2. map() : The map() method of Array 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 ]
    
  3. reduce() : The reduce() method of Array 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
    
  4. reverse() : The reverse() method of Array 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() : The toReversed() method of Array instances is the copying counterpart of the reverse() 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 ]
    
  5. sort() : The sort() method of Array 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() :The toSorted() method of Array instances is the copying version of the sort() 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]
    
0
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

Syed Wasif Hussain
Syed Wasif Hussain