Learn
When working with arrays, we might need to add additional elements to it after initialization.
We can add a new item to the end of an array by calling the array’s .append()
method:
name.append(value)
For example, suppose we have an array called gymBadges
:
var gymBadges = ["Boulder", "Cascade"]
We can add a new item, "Thunder"
, to the end of the array by:
gymBadges.append("Thunder") // ["Boulder", "Cascade", "Thunder"]
Alternatively, append an array of one or more items with the addition assignment operator +=
:
gymBadges += ["Thunder", "Rainbow"] // ["Boulder", "Cascade", "Thunder", "Rainbow"]
Instructions
1.
What are some of your New Year’s resolutions?
Add one more item to the resolutions
array using .append()
.
2.
Add another to the resolutions
array using +=
.
3.
Print out resolutions
.
Is it the same as you expected?
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.