JavaScript Array Methods


Array length
Array toString()
Array pop()
Array push()
Array shift()
Array unshift()
Array join()
Array delete()
Array concat()
Array flat()
Array splice()
Array slice()
The methods are listed in the order they appear in this tutorial page

JavaScript Array length

The length feature tells you how many items are in an array.


JavaScript Array toString()

The JavaScript function toString() changes an array into a string with its values separated by commas.


JavaScript Array join()

The join() method combines all the items in an array into a single string.

It works similarly to the toString() method, but it also lets you choose a separator.


Popping and Pushing

When you use arrays, it's simple to delete or insert new elements.

This is what popping and pushing is:

Removing things from a list, or adding things to a list.


JavaScript Array pop()

The pop() method takes away the final item in an array.

The pop() function gives back the value that was removed.


JavaScript Array push()

The push() function is used to put a new thing at the end of a list or collection.

The push() function provides the length of the updated array:


Shifting Elements

Shifting is like popping, but it deals with the first item, not the last.


JavaScript Array shift()

The shift() function takes out the very first item in an array and moves all the rest of the items to positions with lower index values.

The shift() function gives you the value that was removed from the beginning.


JavaScript Array unshift()

The unshift() method inserts a new element at the start of an array and moves existing elements to make room for it.

The unshift() function gives you the length of the updated array.


Changing Elements

You can get to the items in an array by using their unique index numbers.

Array indexes start with 0:

[0] is the first array element
[1] is the second
[2] is the third ...


JavaScript Array length

The length property is a simple way to add a new item to an array.


JavaScript Array delete()

Warning !

JavaScript lets you remove elements from an array using the delete operator.

When you use the delete keyword, it creates undefined gaps in the array.

Use pop() or shift() instead.


Merging (Concatenating) Arrays

The concat() function makes a fresh array by joining together or combining already existing arrays.

The concat() method leaves existing arrays unchanged. It always returns a new array.

The concat() method can combine multiple arrays, and you can give it as many arrays as you want.

The concat() method can combine strings together as input.


Flattening an Array

Flattening an array means making it simpler by reducing its layers or dimensions.

The flat() function makes a fresh array by joining elements from sub-arrays together to a certain level.


Splicing and Slicing Arrays

The splice() function inserts fresh elements into an array.

The slice() method takes a part from an array.


JavaScript Array splice()

You can use the splice() method to put new things into an array.

The number "2" is the first part of the instruction, indicating where you want to insert new elements (add them) into a list or sequence.

The number 0 in the second parameter tells us how many items should be taken away.

The remaining settings, "Lemon" and "Kiwi," specify the new elements that should be included as additions.

The splice() function gives you back an array containing the items that were removed.


Using splice() to Remove Elements

You can use the splice() function with specific settings to delete items from an array without creating empty spaces in it.

The number "0" in the first parameter tells us where to insert new items (like putting them in a specific place). We call this "adding" the new items.

The number 1 in the second parameter tells us how many things we need to take out.

The rest of the parameters are omitted. No new elements will be added.


JavaScript Array slice()

The slice() method cuts a portion from an array and puts it into a new array.

Note : The slice() method forms a new array.

The slice() method keeps the original array unchanged.

This example takes a piece of a list, starting from the third item, which is "Apple."

You can use the slice() method with two values, just like this: slice(1, 3).

The method chooses elements starting from the position given by the start argument and goes up to, but doesn't include, the position given by the end argument.

If you don't provide the 'end' value, as shown in the initial examples, the slice() method will cut out the remaining part of the array.


Automatic toString()

In JavaScript, when you need a basic value, an array turns into a list of values separated by commas.

When you attempt to display an array, this situation typically arises.

These two examples will produce the same result:

Note : All JavaScript objects have a toString() method.


Finding Max and Min Values in an Array

JavaScript arrays do not have ready-made functions to discover their maximum or minimum values.

In the upcoming chapter of this tutorial, you'll discover the solution to this issue.