Array Methods In JavaScript

In JavaScript, array is a single variable which can hold more than one value at a time. It is often used when we want to store list of elements and access them by a single variable. It’s a more convenient way to store and structure information than is defining of lots of variables with different names.

JavaScript arrays are “dynamic” entities in that they can change size after they are created

Array Methods

  1. Array.push() : Adding Element at the end of an Array.

2. Array.pop() : Removing elements from the end of an array.

3. Array.splice() : method changes the contents of an array by removing existing elements and/or adding new elements.

Syntax:

Parameters:

Start-Index at which to start changing the array (with origin 0).

DeleteCount-(Optional)An integer indicating the number of old array elements to remove.

item1,item2…-(Optional)The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.

4. Array.slice() :method returns a shallow copy of a portion of an array into a new array object selected from begin to end(not included). The original array will not be modified.

Syntax

Parameters

Begin (Optional)Zero-based index at which to begin extraction.

end (Optional)Zero-based index before which to end extraction. Slice extracts up to but not including end

5. Array.reverse() : method reverses an array in place . The first array element becomes the last, and the last array element becomes the first.

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

7. Array.toString(): method returns a string representing the specified array and its elements.

8. Array.sort():method sorts the elements of an array in place, and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

9. Array.length(): The length property of an object which is an instance of type array sets or returns the number of elements in that array.

10. Array.unshift(): The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.