The valueOf() method is the default behavior for an array. It returns an array as a string:
The join() method also joins all array elements into a string.
It behaves just like toString(), but you can specify the separator:
When you work with arrays, it is easy to remove elements and add new elements.
This is what popping and pushing is: Popping items out of an array, or pushing items into an array.
The pop() method removes the last element from an array:
The push() method adds a new element to an array (at the end):
|Remember:  is the first element in an array.  is the second. Array indexes start with 0.|
The pop() method returns the string that was "popped out".
The push() method returns the new array length.
Shifting is equivalent to popping, working on the first element instead of the last.
The shift() method removes the first element of an array, and "shifts" all other elements one place down.
The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:
The shift() method returns the string that was "shifted out".
The unshift() method returns the new array length.
Array elements are accessed using their index number:
The length property provides an easy way to append a new element to an array:
|Using delete on array elements leaves undefined holes in the array. Use pop() or splice() instead.|
The splice() method can be used to add new items to an array:
The first parameter (2) defines the position where new elements should be added (spliced in).
The second parameter (0) defines how many elements should be removed.
The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
With clever parameter setting, you can use splice() to remove elements without leaving "holes" in the array:
The first parameter (0) defines the position where new elements should be added (spliced in).
The second parameter (1) defines how many elements should be removed.
The rest of the parameters are omitted. No new elements will be added.
The sort() method sorts an array alphabetically:
The reverse() method reverses the elements in an array.
You can use it to sort an array in descending order:
By default, the sort() function sorts values as strings.
This works well for strings ("Apple" comes before "Banana").
However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
Because of this, the sort() method will produce incorrect result when sorting numbers.
You can fix this by providing a compare function:
Use the same trick to sort an array descending:
The purpose of the compare function is to define an alternative sort order.
The compare function should return a negative, zero, or positive value, depending on the arguments:
When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
When comparing 40 and 100, the sort() method calls the compare function(40,100).
The function calculates 40-100, and returns -60 (a negative value).
The sort function will sort 40 as a value lower than 100.
How to find the highest value in an array?
And the lowest:
The concat() method creates a new array by concatenating two arrays:
The concat() method can take any number of array arguments:
The slice() method slices out a piece of an array:
The reference contains descriptions and examples of all Array properties and methods.