Array Methods
Converting Arrays to Strings
JavaScript method toString() is used to convert an array to a string of (comma separated) array.
Example:
Convert Arrays to String Using Separator
The join() method is used to join all array elements into a string by using separator
It works just like toString(), but in addition you can specify the separator.
Example:
Popping and Pushing
Popping
Popping means remove the element from an array.
The pop() method removes the last element from an array:
To Remove the First Element from the Array, you can use shift(); method in Place of pop();
Pushing
Pushing means Adding Element to an Array.
The push() method adds a new element to an array (at the end):
Example:
To Remove the Last Element from the Array, you can use unshift(); method in Place of push();
Deleting Elements
To Delete the Element from an Array delete method is used with index of the Element.
Example:
By Using delete method, you can delete any element from an array, you just need to know its Index.
Using delete may leave undefined holes in the array. Use pop() or shift() instead.
Here, When we used the delete it deleted that element and left that space that is why, , is showing at that place. that means there is Element Exist but with Blank Value.
Adding Element at Any Position (Splicing an Array)
The splice() method is used to add new items to an array (at any place):
Syntax
The first parameter initialPosition defines the position where new elements should be added.
The second parameter noOfElements defines how many elements should be removed.
The parameterToAdded define the new elements to be added.
Example
The first parameter (2) defines Adding Elements should start after 2nd Position
The second parameter (0) defines how many elements should be removed.
The ("Brown", "Black") define the new elements to be added after 2nd Position.
Removing Element of Any Position (Splicing an Array)
You can also use splice() to remove elements without leaving "holes" in the array:
The first parameter (0) defines the position where new elements should be added
The second parameter (1) defines how many elements should be removed.
This is Recommended Method, to Remove Element at any Index.
Merging (Concatenating) Arrays
Concatenation Refers to Merging Arrays.
The concat() method creates a new array by merging existing arrays:
Syntax:
Example:
Here,
frd2 , frd3 Array is added to the frd1
Slicing(Cutting) an Array
The slice() method cuts out a piece of an array into a new array.
Syntax:
Here,
slice(a,b) method cuts out the element between the index a and index b
Sorting Array
Sorting Alphabetically
The sort() method is used to sort an array alphabetically
Syntax
Example:
Reverse Sorted
The reverse() method is used to sort an array in Reverse Order of the Entered Array.
Syntax
Example:
Find The Index of Element By Value
The indexOf() method searches an array for an element value and returns its position.
Example:
If any Element is present more than once in Array List, it returns the position of the first occurrence.
The Method Array.indexOf() returns -1 if the Element is not found.
Find The Index of Last Occurrence of Element By Value
Array.lastIndexOf() returns the position of the last occurrence of the specified element.
Example: