-
Notifications
You must be signed in to change notification settings - Fork 0
/
.append()
32 lines (17 loc) · 898 Bytes
/
.append()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"]
var resolutions = ["play more music 🎸",
"read more books 📚",
"drink more water 💧"]
// Write your code below 💪
resolutions.append("Get fit")
resolutions += ["Lose weight"]
print(resolutions) // ["play more music 🎸", "read more books 📚", "drink more water 💧", "Get fit", "Lose weight"]