Skip to main content

Command Palette

Search for a command to run...

Array Methods In JavaScript

Published
•4 min read•View as Markdown
Array Methods In JavaScript

Different array methods are as follows :

  1. Length: As its name suggests that it defines the length of an array.

    let abc=["a", "b", "c", "d"];
    console.log(abc);
    console.log(abc.length);
    
  2. Push: It is an array method which is used to push or we can say to add an element to the array.

    let abc=["a", "b", "c", "d"];
    abc.push("e");
    console.log(abc);
    
  3. Slice: As after reading the word slice we get the meaning of that, it is exactly the same. it will cut the element from the array but the original array will not get affected due to this. first, we have to write the array name and after that .slice and then in the bracket, we have to define the index from which to where we want to cut the array. an example is shown below.

    let abc=["a", "b", "c", "d"];
    console.log(abc.slice(1,3));
    console.log(abc);
    
  4. Splice: Using this method we can add the element just like a push but in this, we can define where we want to add the element i.e. position where we want add it and

    at the same time how many elements do we want to remove from the array after that position

    let abc=["a", "b", "c", "d","b", "c","b", "c","z" ];
    abc.splice(2, 4, "e", "f")
    console.log(abc);
    

    in the above example, 2 is an index where we want to add an element and 4 is the number of elements we want to remove after that index.

  5. Concatenation: It is a method which is used to add two or more arrays.

    let abc=["a", "b", "c", "d","b", "c","b", "c","z" ];
    let durgesh=[1,2,3,4,8,45,75];
    let fog=["c","b", "c","z"];
    console.log(abc.concat(durgesh));
    console.log(abc.concat(durgesh,fog));
    
  6. Fill: It is a method which helps us to replace the new element in place of the existing one.

    let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    arr4.fill("durgesh",2, 5, 7,9);
    console.log(arr4);
    
  7. Include: It is the method used to check the element of an array is present or not at the given position, if it is then it will give output as true or else it is false.

    let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    console.log(arr4.includes(7,6));
    
  8. IndexOf: It is the method which is used to know the index of a particular element of an array.

    let arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    console.log(arr4.indexOf(5));
    
  9. isArray: It is a method which is used to check whether the particular variable is an array or not.

    let num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8, 'Anurag', 'Anurag'];
    let num1 = 'Anurag';
    console.log(Array.isArray(num1));
    console.log(Array.isArray(num));
    
  10. Join: With the help of this, some element is joined after the particular element of an array. and output will be in string format

    Join
    let Arr1 = [1, 2, 3, 4, 5, 6, 7];
    let var1 = Arr1.join(' 1 ');
    console.log(var1);
    console.log(typeof var1);
    
  11. lastindexOf: It will give the index of the element of an array.

    let Arr1 = [1, 2, 3, 4, 5, 6, 7];
    console.log(Arr1.lastIndexOf(6));
    
  12. Map: Whatever method used in this is applicable to each and every elment of the array.

    Map
    let maths = [1, 4, 9, 16, 25];
    console.log(maths.map(Math.sqrt));
    
  13. POP: It is used to remove an element from the end of an array

    let maths = [1, 4, 9, 16, 25];
    console.log(maths.pop());
    console.log(maths);
    
  14. Reverse: It is used to reverse the order of the array.

    let maths = [ 1, 4, 9, 16, 25];
    console.log(maths.reverse());
    
  15. Shift: It is used to remove the first element from the array.

    let maths = [ 1, 4, 9, 16, 25];
    console.log(maths.shift());
    console.log(maths);
    
  16. Sort: It is used to arrange elements in ascending order

    let fruit = ['Apple', 'oada Apple', 'Chota Apple', 'Double Apple'];
    console.log(fruit.sort());
    
  17. Unshift: It is a method which is used to add an element at the start of an array.

    let fruit = ['Apple', 'Bada Apple', 'Chota Apple', 'Double Apple'];
    fruit.unshift('Apple 1', 'Grapes 2');
    console.log(fruit);
    
  18. Split: It is a method which is used to convert a string to an array.

    let num = "Durgesh"
    let a = num.split("");
    console.log(a);