Skip to content

Instantly share code, notes, and snippets.

@romelperez
Created February 1, 2015 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romelperez/7e326ecdcef0af6858ee to your computer and use it in GitHub Desktop.
Save romelperez/7e326ecdcef0af6858ee to your computer and use it in GitHub Desktop.
JavaScript Array Methods
// ARRAY METHODS //
// array.concat(item...)
var a = ['a', 'b', 'c'];
var b = ['x', 'y', 'z'];
var c = a.concat(b, true);
// c is ['a', 'b', 'c', 'x', 'y', 'z', true]
// array.join(separator)
var a = ['a', 'b', 'c'];
var b = a.join(''); // c is 'abc'
// array.reverse()
var a = ['a', 'b', 'c'];
var b = a.reverse();
// both a and b are ['c', 'b', 'a']
// array.push(item...)
var a = ['a', 'b', 'c'];
var b = ['x', 'y', 'z'];
var c = a.push(b, true);
// a is ['a', 'b', 'c', ['x', 'y', 'z'], true] & c is 5
// array.unshift(item...)
var a = ['a', 'b', 'c'];
var r = a.unshift('?', '@');
// a is ['?', '@' 'a', 'b', 'c'] & r is 5
// array.pop()
var a = ['a', 'b', 'c'];
var b = a.pop(); // a is ['a', 'b'] & b is 'c'
// array.shift()
var a = ['a', 'b', 'c'];
var b = a.shift(); // a is ['b', 'c'] & b is 'a'
// array.slice(start, end)
var a = ['a', 'b', 'c'];
var b = a.slice(0, 1); // b is ['a']
var c = a.slice(1); // c is ['b', 'c']
var d = a.slice(1, 2); // d is ['b']
// array.splice(start, deleteCount, item...)
var a = ['a', 'b', 'c'];
var r = a.splice(1, 1, 'ache', 'bug');
// a is ['a', 'ache', 'bug', 'c'] & r is ['b']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment