JavaScript Array Methods

Converting Arrays to Strings

The JavaScript method toString() converts an array to a string of (comma separated) array values.
The join() method also joins all array elements into a string.

Popping and Pushing

Popping items out of an array, or pushing items into an array.
pop() method removes the last element.
push() method adds a new element to an array (at the end).

Shifting Elements

shift() method removes the first array element and "shifts" all other elements to a lower index.
 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.

Splicing an Array

splice() method can be used to add new items to an array:

Example :

var fruits = ["Banana""Orange""Apple""Mango"];
fruits.splice(20"Lemon""Kiwi");

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.

Reversing and Sorting an Array

sort() method sorts an array alphabetically.
reverse() method reverses the elements in an array.

Comments

Popular posts from this blog

Bootstrap Breakpoints