Search within TutorialsPark

Javascript Array Object Methods


Learn to manipulate array objects using array methods


Javascript Arrays: Methods

Like all other objects in JavaScript, the arrays too can be have a list of methods which can be used to manipulate it .

These methods can be used for various purposes like, adding new elements, removing elements, joining two arrays etc.

Table: JavaScript Arrays : Methods of an Array

Properties Description
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.

JavaScript Arrays: Method Concat()

The Javascript Array Method concat() is used to join two array objects and create a new array which is result of concatenation of two arrays.

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

Syntax: Javascript Arrays : Concat() Method.

Example: Javascript Arrays : Concat() Method.

Give it a TRY! » Note: The result of the two concatenation is returned to variable calc.


Javascript Arrays: Method pop()

The Javascript Array Method pop() is used to delete the last element of an array, decrement the length of the array, and then return the value of the element that got deleted.

Incase the array is empty, the method pop() has no effect on the array and returns an undefined value.

Syntax: JavaScript Arrays: Method pop()

Example: JavaScript Arrays : Method pop()

Give it a TRY! » Note: Always the last element of the array is removed.


Javascript Arrays: Method push()

The Javascript Array Method push() is used to append the arguments at the end of the array, and returns the length of the array.

It doesn't create a new array, but instead modifies the existing array.Its mostly used with the method pop()

Example: JavaScript Arrays : Method push()

Example: JavaScript Arrays : Method push()

Give it a TRY! » Note: The elements are pushed always at the end of the array.


Javascript Arrays: Methods shift() and unshift()

The Javascript Array Method shift() is used to remove the first element of the array and shifting all subsequent elements downward to occupy the vacant space created at the start .

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 Javascript Array Method unshift() is used to a insert elements at the beginning of an array, shifting all subsequent element to higher indexes to make room

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

Example: JavaScript Arrays : shift() and unshift()

Example: JavaScript Arrays : unshift() and shift()

Give it a TRY! » Note: Elements are added and removed from the beginning of the array.


Javascript Arrays: slice() Method

The Javascript Array Method slice() is used to slice some portion of an array and then return the subarray.

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

Syntax: JavaScript Arrays: slice()

Example: JavaScript Arrays : slice()

Give it a TRY! » Note: The slice() operation has no effect on the original array.


Javascript Arrays: Conversion Methods

The JavaScript methods toLocaleString(), valueOf(), toString() are applicable on all javascript objects.

The below demo has two objects, country1 and country2, each defines both toString() and toLocaleString() methods.

Syntax: JavaScript Arrays: Conversion Methods

Example: JavaScript Arrays : Conversion Methods

Give it a TRY! » Note: Each method returns the array items in a comma seperated string.


Javascript Arrays: Stack Datastructure

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() .

Example: JavaScript Arrays : stack Method

Give it a TRY! » Note: The fifth item is assigned directly to the index position 4.


Javascript Arrays: Queue Datastructure

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.

Example: JavaScript Arrays : Queue Method

Give it a TRY! » Note: The method shift() along with push() is used to emulate the queue datastructure .


Javascript Arrays: Reordering

For reordering the items within an array two methods can be used : reverse() and sort().

Syntax: JavaScript Arrays: reverse() and sort()

Example: JavaScript Arrays : reverse() and sort()

Give it a TRY! » Note: The sort() method arranges the elements in ascending order.


Javascript Arrays: Location Method

The Javascript location methods of indexOf() and lastIndexOf() are used to search items beginning from front of the array and end of the array respectively.

Syntax: JavaScript Arrays: Reordering

Example: JavaScript Arrays : Reordering

Give it a TRY! » Note: Use these methods to locate specific items within arrays.