Professional Documents
Culture Documents
indexOf()
Use indexOf() to find the position of an element:
var array = ['one', 'two', 'three'];
array.indexOf('two'); // 1
array.indexOf('four'); // -1
Array.push()
Use push() to append an element to the end of an array:
var array = [];
array.push('foo');
array; // [ 'foo' ]
Array.pop()
Use pop() to remove the last element from an array:
var array = ['first', 'last'];
array.pop();
array; // [ 'first' ]
Array.unshift()
Use unshift() to insert an element to the beginning of an array:
var array = ['foo'];
array.unshift('first');
array; // [ 'first', 'foo' ]
Array.shift()
Use shift() to remove the first element from an array:
var array = ['first', 'last'];
array.shift();
array; // [ 'last' ]
Array.slice()
Use slice() to extract element(s) from an array:
var array = ['one', 'two', 'three'];
var start, end;
Because the method returns a shallow copy of the array, this means
that objects are passed by reference.
Array.splice()
Use splice() to mutate an array:
var array = ['one', 'two', 'three'];
// remove elements from index 1 to end
array.splice(1); // [ 'two', 'three' ]
array; // [ 'one' ]