push()

🎉
💨
Time to add new cars to our train! push() is super useful!

The push() Method

The push() method adds a new item to the end of an array.

It's like attaching a new train car to the back of your train!

💡
💨

How push() Works

1const numbers = [1, 2, 3];
2
3// Add 4 to the end
4numbers.push(4);
5
6console.log(numbers);
7// [1, 2, 3, 4]
8
9// push() returns the new length
10const length = numbers.push(5);
11console.log(length); // 5

Watch it Happen!

[
1
[0]
2
[1]
3
[2]
]
length: 3
1

Start with an array

We have an array with numbers [1, 2, 3]

🎮

Try It Yourself!

Type a value and push it onto the array! Watch it slide in from the right.

[
1
[0]
2
[1]
3
[2]
]
length: 3