Skip to main content

JavaScript Array

Published 7 min readMarkdown
JavaScript Array

JavaScript array is an object that represents a collection of similar type of elements.

//Syntax
const myArr = ["ram"," mohan","shyam"];
console.log(myArr);
// Output: [ ram, mohan , shyam]

JavaScript Array Methods

  1. Array.Prototype.at()
    The at() method takes an integer value. and return the item at that index.

     //Example
     const array1 = [ 5, 12, 8, 130, 44];
     const index = 2;
     const array2 = array1.at(index);
     console.log(array2);
     //Output : 8
    
  2. Array.Prototype.concat()
    The concat() method is used to merge two or more arrays. this method doesn't change the existing arrays but instead returns a new array.

     const arr1 = [1,2,3];
     const arr2 = [4,5,6];
     const arr3 = arr1.concat(arr2);
     console.log(arr3);
     //output : [1, 2, 3, 4, 5, 6]
     const arr4 = arr2.concat(arr1);
     //output : [4, 5, 6, 1, 2, 3]
    
  3. Array.Prototype.every()
    The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

     const  arr1 = [1, 2, 3 ,4 ,5];
     const arrCheck = (num) => num<10;
     console.log(arr1.every(arrCheck));
     //Output : true
     const  arr2 = [1, 4, 5, 12, 17];
     const arrCheck = (num) => num<10;
     console.log(arr2.every(arrCheck));
     //Output : False
    
  4. Array.Prototype.fill()
    The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array.

     const arr1 = [1, 2, 3 ,4 ,5];
     console.log(arr1.fill(6));
     //output : [6, 6, 6, 6, 6]
     console.log(arr1.fill(6,2));
     //output : [1 ,2, 6, 6, 6]
     console.log(arr1.fill(6,1,3))
     //output : [1, 6, 6, 6, 5]
    
  5. Array.Prototype.find()
    The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

     const array1 = [5, 12, 8, 57, 43];
     const found = array1.find(element => element > 10);
     console.log(found);
     //  output: 12
    
  6. Array.Prototype.filter()

    The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

     const array1 = [5, 12, 8, 57, 43];
     const found = array1.filter(element => element > 10);
     console.log(found);
     //output: [12, 57, 43]
    
  7. Array.Prototype.findIndex()
    The findIndex() method 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.

     const array1 = [5, 12, 8,45, 57, 43];
     const isLargeNumber = (element) => element > 13;
     console.log(array1.findIndex(isLargeNumber));
     //output : 3
    
  8. Array.Prototype.findLastIndex()
    The findLastIndex() method 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.

     const array1 = [5, 62, 8, 55, 57, 4];
     const isLargeNumber = (element) => element > 50;
     console.log(array1.findIndex(isLargeNumber));
     //output : 4
    
  9. Array.Prototype.flat()
    The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

     const arr1 = [0, 1, 2, [3, 4]];
     console.log(arr1.flat());
     // output: Array [0, 1, 2, 3, 4]
     const arr2 = [0, 1, 2, [[[3, 4]]]];
     console.log(arr2.flat(2));
     // output: Array [0, 1, 2, Array [3, 4]]
     console.log(arr2.flat(3))
     //output [0, 1, 2, 3, 4]
    
  10. Array.Prototype.forEach()
    The forEach() method executes a provided function once for each array element of the given array.

    const arr1 = [ 1, 2, 3, 4];
    const mult = array1.forEach(element => element*2);
    console.log(mult);
    //output : [2, 4, 6, 8]
    
  11. Array.Prototype.includes()

    The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

    const array1 = [1, 2, 3];
    console.log(array1.includes(6))
    //false
    console.log(array1.includes(2))
    //true
    
  12. Array.Prototype.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.

    const array1 = [5, 62, 8, 55, 57, 55];
    console.log(array1.indexOf(55));
    // 3
    console.log(array1.indexOf(65));
    // -1
    console.log(array1.indexOf(55,4));
    // 5
    
  13. Array.Prototype.join()
    The join() method creates and returns a new string by concatenating all of the elements in an 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());
    // output: "Fire,Air,Water"
    
    console.log(elements.join(''));
    // output: "FireAirWater"
    
    console.log(elements.join('-'));
    // output: "Fire-Air-Water"
    
  14. Array.Prototype.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 backward, starting at fromIndex.

    const array1 = [5, 62, 8, 55, 57, 55];
    console.log(array1.lastIndexOf(55));
    // 5
    console.log(array1.lastIndexOf(65));
    // -1
    
  15. Array.Prototype.map()

    The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

    const array1 = [2 ,3, 5, 7, 8]
    const map1 = array1.map(x => x * 2);
    console.log(map1);
    // output: Array [4, 6, 10, 14 , 16]
    
  16. Array.Prototype.pop()

    The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    
    console.log(plants.pop());
    // output : "tamato"
    console.log(plants);
    // output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
    
  17. Array.Prototype.push()

    The push() method adds one or more elements to the end of an array and returns the new length of the array.

    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale'];
     const counts = plants.push("tamato"));
    console.log(counts);
    // output : 5
    console.log(plants);
    //output: Array ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    
  18. Array.Prototype.reverse()
    The reverse() method reverses an array 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.

    const array1 = ['one', 'two', 'three'];
    const reversed = array1.reverse();
    console.log( reversed );
    //Array ["three", "two", "one"]
    
  19. Array.Prototype.shift()

    The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    
    console.log(plants.shift());
    // output : "broccoli"
    console.log(plants);
    // output: Array ["cauliflower", "cabbage", "kale", "tamato"]
    
  20. Array.Prototype.unShift()
    The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

    const array1 = [1, 2, 3];
    console.log(array1.unshift(4, 5));
    // output: 5
    console.log(array1);
    // output: Array [4, 5, 1, 2, 3]
    
  21. Array.Prototype.some()
    The some() method 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.

    const array = [3, 2, 7, 4, 5];
    const even = (element) => element % 2 === 0;
    console.log(array.some(even));
    // Expected output: true
    
  22. Array.Prototype.slice()
    The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    console.log(animals.slice(2));
    // output: Array ["camel", "duck", "elephant"]
    console.log(animals.slice(2, 4));
    // output: Array ["camel", "duck"]
    console.log(animals.slice(1, 5));
    // output: Array ["bison", "camel", "duck", "elephant"]
    console.log(animals.slice(-2));
    // output: Array ["duck", "elephant"]
    console.log(animals.slice(2, -1));
    // output: Array ["camel", "duck"]
    console.log(animals.slice());
    // output: Array ["ant", "bison", "camel", "duck", "elephant"]
    
  23. Array.Prototype.splice()
    The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements.

    const months = ['Jan', 'March', 'April', 'June'];
    months.splice(1, 0, 'Feb');
    // Inserts at index 1
    console.log(months);
    // output: Array ["Jan", "Feb", "March", "April", "June"]
    months.splice(4, 1, 'May');
    // Replaces 1 element at index 4
    console.log(months);
    // output: Array ["Jan", "Feb", "March", "April", "May"]
    
  24. Array.Prototype.toString()

    The toString() method returns a string representing the specified array and its elements.

    const array1 = [1, 2, 'a', '1a'];
    
    console.log(array1.toString());
    // output: "1,2,a,1a"
    
  25. Array.isArray()

    The Array.isArray() static method determines whether the passed value is an Array.

    console.log(Array.isArray([1, 3, 5]));
    // output: true
    console.log(Array.isArray('[]'));
    // output: false
    
  26. Array.Prototype.reduce()
    The reduce() method 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.

    const array1 = [1, 2, 3, 4, 5];
    
    // 0 + 1 + 2 + 3 + 4
    const initialValue = 0;
    const sumWithInitial = array1.reduce(
      (accumulator, currentValue) => accumulator + currentValue,
      initialValue
    );
    console.log(sumWithInitial);
    // Expected output: 15
    

No comments yet. Be the first to comment.