arr.toString()

returns a string containing each entry of the array seperated by a ‘,’.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.toString();
// ==> 'Tomato,Cabage,Lettuce,Cucumber'

arr.join(xxx)

similar to .toString(), but accepts a string as an argument that will be used to seperate the elements of the array instead of ‘,’

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.join(' *~* ');
// ==> 'Tomato *~* Cabage *~* Lettuce *~* Cucumber'

arr.pop()

pops an element out of an array. removes the last element of the array and returns it. this method alters the original array.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.pop();
// ==> 'Cucumber'

arr.push(xxx)

pushes a new element to the end of an array. returns the new length of the array.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.push('Artichoke');
// ==> 5
// vegis = [ 'Tomato', 'Cabage', 'Lettuce', 'Cucumber', 'Artichoke' ];

arr.shift()

works similar to .pop(), but removes the first element of the array instead of the last.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.shift();
// ==> 'Tomato'
// vegis = [ 'Cabage', 'Lettuce', 'Cucumber' ];

arr.unshift(xxx)

adds a new element the front of an array. returns the new length of the array.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.unshift('Artichoke');
// ==> 5
// vegis = [ 'Artichoke', 'Tomato', 'Cabage', 'Lettuce', 'Cucumber' ];

delete arr[x]

removes an element out of an array based of the specified index. delete leaves undefined holes in the array, therefor .pop()/.shift() should be used instead.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
delete vegis[1];
// ==> true
// vegis = [ 'Tomato', , 'Lettuce', 'Cucumber' ]

arr.splice(xxx, yyy, www, zzz)

is used to add/remove elements to an array. adding items to an array takes 3+ args, where removing only takes 2 args.

lets take a look at how to add elements to an array with .splice()

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.splice(2, 0, 'Artichoke', 'Salad');
// ==> [] // returns array of removed elements
// vegis = [ 'Tomato', 'Cabage', 'Artichoke', 'Salad', 'Lettuce', 'Cucumber' ]

1st arg: defines where the new elements should be added in (spliced in) 2nd arg: defines how many elements should be removed. 3+ : takes unlimited args, which are to be added to the array.

removing elements with .splice() only takes 2 args, where to begin the splice and how many elements to remove. lets take a look:

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.splice(0, 2);
// ==> [ 'Tomato', 'Cabage' ] // returns the removed elements
// vegis = [ 'Lettuce', 'Cucumber' ];

arr.sort()

sorts the array alphabetically. by default, sorts the values of strings.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.sort();
// ==> [ 'Cabage', 'Cucumber', 'Lettuce', 'Tomato' ]

to sort numeric values, we need to provide a compare function:

var vals = [202, 42, 89, 1, 72, 404, 420];
vals.sort(function(a, b) { return a > b });
// ==> vals = [ 1, 42, 72, 89, 202, 404, 420 ]

var vals = [202, 42, 89, 1, 72, 404, 420];
vals.sort(function(a, b) { return b > a });
// ==> vals = [ 420, 404, 202, 89, 72, 42, 1 ]

arr.reverse()

reverses the elements in an array. is commonly used to sort the array in descending order.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.sort();
vegis.reverse();
// ==> [ 'Tomato', 'Lettuce', 'Cucumber', 'Cabage' ]

arr.concat(xxx, yyy)

creates a new array by joining 2 arrays

var boys = ['Matt', 'Paul', 'James'];
var girls = ['Lucy', 'Katie', 'Molly'];

var ppl = boys.concat(girls);
// ==> ppl = [ 'Matt', 'Paul', 'James', 'Lucy', 'Katie', 'Molly' ];

arr.slice(xxx, yyy)

slices out a peice of an array and into a new array. can take 1 or 2 elements. with 1 element, it slices from the index to the end of the array. does not mutate the array.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.slice(2);
// ==> [ 'Lettuce', 'Cucumber' ]
// vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];

with 2 args, it starts with the 1st arg, and ends (not including) the 2nd arg.

var vegis = ['Tomato', 'Cabage', 'Lettuce', 'Cucumber'];
vegis.slice(1, 3);
// ==> [ 'Cabage', 'Lettuce' ]

arr.valueOf();

the default value of an array. it converts an to a primitive value