.push()
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:
Contribute to Docs
- 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.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours