.push()
christian.dinh2476 total contributions
Published Jun 18, 2021Updated Jul 11, 2022
Contribute to Docs
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:
All contributors
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributorAnonymous contributor95 total contributions
- BrandonDusch580 total contributions
- christian.dinh
- Anonymous contributor
- Anonymous contributor
- BrandonDusch
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.