Table of contents
No headings in the article.
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.
Pop :
The pop() method removes an element from the end of an array.
Shift :
The shift() method removes an element from the start of an array(i.e removes the first array element).
Unshift :
The unshift() method adds an element at the start of an array(i.e. adds an element at the beginning of an array).
Length :
The length property gives the length of an array. We can also set the length of an array.
Concat :
The concat() method adds one array to another array. (i.e it merges two arrays).
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.
Reverse :
The reverse() method reverses the order of the elements of an array.
IndexOf :
The indexOf() method searches an element inside the array and returns its index.
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.
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.
Is array :
The isArray() method returns true if an object is an array, otherwise returns false.
Fill :
The fill() method fills specified elements in an array with a value. We can give start and end positions.
Map :
The map() method takes each element of an array and does the required operations on it.
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(,).