Skip to main content Accessibility Feedback

Adding items to an array with vanilla JavaScript

You can use the push() method to add items to an array.

var sandwiches = ['turkey', 'tuna', 'blt'];
sandwiches.push('chicken', 'pb&j');

// returns ['turkey', 'tuna', 'blt', 'chicken', 'pb&j']

Merging two arrays together

What if you want to add an array of values rather than a single value? You can use Array.prototype.push.apply() to merge two or more arrays together. It merges all subsequent arrays into the first.

var sandwiches1 = ['turkey', 'tuna', 'blt'];
var sandwiches2 = ['chicken', 'pb&j'];
Array.prototype.push.apply(sandwiches1, sandwiches2);

// returns ['turkey', 'tuna', 'blt', 'chicken', 'pb&j']

// returns ['chicken', 'pb&j']

Browser Compatibility

These techniques work in all modern browsers, and back to at least IE6.