Skip to main content

Javascript Array and its Methods

Updated 3 min readMarkdown
Javascript Array and its Methods

Array :

  • JavaScript array is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable.

  • In the javascript array, we can store elements with different data types.

  • eg. let arr=["Fruits", 100, true, "Flower", 40]

  • We can declare an array in the javascript using the following methods,

  • let arr = ["Banana", "Orange", "Apple", "Grapes"];

  • let arr = new array("Banana", "Orange", "Apple", "Grapes");

Array Methods :

  • Push :

    The Push() method adds an element at the end of an array.

    %[https://codepen.io/rohan1797/pen/WNKeyQQ]

  • Pop :

    The pop() method removes an element from the end of an array.

    %[https://codepen.io/rohan1797/pen/gOjYKPN]

  • Shift :

    The shift() method removes an element from the start of an array(i.e removes the first array element).

    %[https://codepen.io/rohan1797/pen/BaPBVZN]

  • Unshift :

    The unshift() method adds an element at the start of an array(i.e. adds an element at the beginning of an array).

    %[https://codepen.io/rohan1797/pen/NWBKzYR]

  • Length :

    The length property gives the length of an array. We can also set the length of an array.

    %[https://codepen.io/rohan1797/pen/abjoKja]

  • Concat :

    The concat() method adds one array to another array. (i.e it merges two arrays).

    %[https://codepen.io/rohan1797/pen/KKBPeGv]

  • Slice :

    • The slice() method slices out elements of an array into a new array.

    • This method creates a new array. This method does not remove any elements from the source array.

    • This method can take two arguments like slice(1, 3).

    • The method then selects elements from the start argument, and up to (but not including) the end argument.

  • Splice :

    • The splice() method is used to add new elements to an array.

    • const fruits = ["Banana", "Orange", "Apple", "Mango"];

    • fruits.splice(2, 0, "Lemon", "Kiwi");

    • The first parameter (2) defines the position where new elements should be added.

    • The second parameter (0) defines how many elements should be removed.

    • The rest of the parameters ("Lemon", "Kiwi") define the new elements to be added.

    • If we omit the add element parameter then it only removes elements from an array.

%[https://codepen.io/rohan1797/pen/xxJKBYK] 
  • Sort :

    The sort() method rearranges the elements of an array in alphabetical order.

    %[https://codepen.io/rohan1797/pen/oNMvVax]

  • Reverse :

    The reverse() method reverses the order of the elements of an array.

    %[https://codepen.io/rohan1797/pen/YzjKgdN]

  • IndexOf :

    The indexOf() method searches an element inside the array and returns its index.

    %[https://codepen.io/rohan1797/pen/qByWvvb]

  • lastIndexOf :

    The lastIndexOf() method gives the last index of a given element inside an array.

    This method searches the given element in the array and finds the last occurrence of a given element and returns the index of that position.

    %[https://codepen.io/rohan1797/pen/eYjOXwq]

  • includes :

    The includes() method searches a given element in the array and returns true if a given element is present in the array else it returns false.

    %[https://codepen.io/rohan1797/pen/ExpYJaV]

  • Is array :

    The isArray() method returns true if an object is an array, otherwise returns false.

    %[https://codepen.io/rohan1797/pen/YzjXoxg]

  • Fill :

    The fill() method fills specified elements in an array with a value. We can give start and end positions.

    %[https://codepen.io/rohan1797/pen/RwBPzxz]

  • Map :

    The map() method takes each element of an array and does the required operations on it.

    %[https://codepen.io/rohan1797/pen/oNMXrym]

  • Join :

    The join() method returns an array as a string. All the elements of an array join by using the mentioned separator. The default separator is a comma(,).

    %[https://codepen.io/rohan1797/pen/WNKvqPp]

No comments yet. Be the first to comment.