.push()
The .push()
method adds one or more elements to the end of an array and returns the new length.
Syntax
array.push(item1, item2, ...itemN);
A comma-separated list of items (strings, variables, or functions) can be passed to the end of the array
. The .push()
method is not to be confused with returning an entirely new array with the passed object.
Example
Add one item to the end of the colors
array:
const colors = ['red', 'orange'];colors.push('yellow');console.log(colors);
This will output the following:
["red", "orange", "yellow"]
Codebyte Example
The following example adds multiple colors to the colors
array: