Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why can't I use this code instead of the teacher's code? https://teamtreehouse.com/library/the-solution-29

in the video, teacher offered the first answer like this.

https://teamtreehouse.com/library/the-solution-29

teachers.push(...newTeachers);

but Why can I use this code?

teachers.push(newTeachers);

of course, this code doesn't get the right result.

What is the difference between the two? I can't find any lecture about the "..."?

Hi Kisu,

Give this documentation a read. The ... is what is called a Spread operator or Rest parameter.

Here is a link to the Treehouse video.

1 Answer

Steven Parker
Steven Parker
243,656 Points

The argument "newTeachers" is an array, but the "push" method takes multiple individual arguments. An ellipsis ("...") is the "spread" operator that converts an array into individual arguments.

While not covered in detail in this short workshop, the instructor does give you a short explanation of the operator function in the video. Other courses such as Introducing ES2015 (as suggested by Ryan) cover this operator in detail.

Thank you for detailed explanation!