push()
Array
instance method setterS2C Home
« Globals « Array « push()
Description
Adds element(s) to the end of an array and returns the new length of the array.
Syntax
Signature | Description |
---|---|
anArray.push(element1, ..., elementN) | Adds element(s) to the end of an array and returns the new length of the array. |
Parameters
Parameter | Description |
---|---|
element1, ..., elementN | The element(s) to add to the end of the array. |
Examples
The code below creates an array then adds some elements to the end.
/*
Create an array of names.
Push two elements on to end of array.
names = ['Fred', 'Ned', 'Ted', 'Jed', 'Neil'] after push().
Variable arrayLength = 5 after push().
*/
var names = ['Fred', 'Ned', 'Ted'];
var arrayLength = names.push('Jed', 'Neil');
alert(names + ' - ' + arrayLength);
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 1 - Arrays