Learn to manipulate array objects using array methods
These methods can be used for various purposes like, adding new elements, removing elements, joining two arrays etc.
|push()||To push(add) elements at the end of an array.
|pop()||To pop(remove) and return the last elements of an array
|concact()||To concat elements elements from one array to another array.|
|join()||To join the elements of an array using a seperator to form a string.|
|shift()||To remove and return the first element of an array .|
|slice()||To create a new array from elements of an existing array|
|sort()||To sort an array either alphabetically or numerically.|
|splice()||To remove or replace the elements of an array.|
|reverse()||To reverse the order of elements in an array.|
|toLocaleString()||To return the string representation of a given array in a local format.|
|toString()||To return the string representation of a given array|
|unShift()||To add elements at the start of an array.|
The behaviour of the two arrays is not altered due to concatenation, if an array is used as an argument in the concat() method , then the items of the array are concatenated rather than the array itself
Incase the array is empty, the method pop() has no effect on the array and returns an undefined value.
It doesn't create a new array, but instead modifies the existing array.Its mostly used with the method pop()
It does not create new array, instead modifies the array directly. It functions similar to pop() but operates at start instead at the end of array
The method is used in conjection with shift() method. The first element to shift() becomes the new element 0 of the the array. It does not create a new array instead it modifies the original array
The array returned consists of elements specified at the start and all subsequent elements up to, but not including the end element.
It does not modify the original array, instead the sliced elements are a part of the new array
The below demo has two objects, country1 and country2, each defines both toString() and toLocaleString() methods.
Arrays can be used to create multiple types of datastructures as well,for eg: stack datastructure, it uses a last-in-fast-out(LIFO) rule to add/remove elements
In a Stack datastructure, the most recent added item in the array is the first to be removed, insertion and removal is done using method push() and pop() .
A Queue is a datastructure which restricts access using a first-in-first-out(FIFO) data structure.
New elements are added to the end of the list and retrieved from the front of queue. Eg: a bus queue.
For reordering the items within an array two methods can be used : reverse() and sort().