push()  Array  instance method JS Home  <<  JS Reference  <<  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, ..., elementNThe 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);

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 1 - Arrays

JavaScript Basics

JavaScript Basics

JavaScript Intermediate

JavaScript Intermediate

JavaScript Advanced

JavaScript Advanced

JavaScript Reference

JavaScript Entities

Globals

Array

Class Properties
prototype
Instance Properties
constructor
length
Class Methods
None.
Instance Methods Getters
concat()
join()
slice()
toString()
Instance Methods Setters
pop()
push()
reverse()
shift()
sort()
splice()
unshift()

Statements

Operators