An array can be created by enclosed the items of it within square brackets. For this example, we’ll imagine that we are looking at a list of fruits.
let fruits = [“mango”, “apple”, “orange”];
where “mango”, “apple”, “orange” each of them are array elements and that means the length of the array is 3.
Methods of Array:
There are several built-in methods to use in arrays, and we will go over a few of the most commonly used ones.
The map method is used to create a new array from an existing one by applying a function to each of the elements of the first array. It does not change the original array.
The filter method applies the function that returns one new array with the entire set of elements that satisfies the condition inside the provided function.
The forEach method is used to get all elements in array one by one without using a loop. A callback function is added as a parameter in forEach.
The find method only returns the value of the first element that satisfies the condition inside the provided function.
The reduce method executes a function against an accumulator and each of the array elements to reduce it to a single value.
The sort method simply allows us to arrange the elements of an array and returning the sorted array.
The indexOf method returns the index of the first element in the array or -1 if it is not found. Remember in array index starts from 0.
push() ,pop(),shift() & unShift()
The push method used to add one or more elements at the end of the array and return a new array.
The pop method used to delete the last element from the array and return that deleted one.
Similarly, The shift method used to delete the first element from the array and return that deleted one.
The unShift method used to add one or more elements at the beginning of the array and return a new array.
Next, we will discuss about how to add or delete any element at any position and also some more interesting array methods.