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 trialHeidi Fryzell
Front End Web Development Treehouse Moderator 25,178 PointsI understand what the spread operator does, but what would be the longer version?
function addNewTeachers(newTeachers) {
// TODO: write a function that adds new teachers to the teachers array
teachers.push(...newTeachers);
};
I know that the spread operator converts a collection of items into individual elements. So above it is saying, add all the elements of newTeachers array to the teachers array. But what would be the longer way to write it out "...newTeachers", if you didn't use the spread operator? I want to understand what the spread operator is short hand for, exactly.
1 Answer
Steven Parker
231,269 PointsIf you didn't have the spread operator, you might use a loop to do the same job:
for (var i=0; i<newTeachers.length; i++)
teachers.push(newTeachers[i]); // push one at a time
Heidi Fryzell
Front End Web Development Treehouse Moderator 25,178 PointsHeidi Fryzell
Front End Web Development Treehouse Moderator 25,178 PointsThanks Steven Parker ! That was exactly what I wanted to know.
Robert Young
11,579 PointsRobert Young
11,579 PointsSteven Parker damn I need to know more about that spread thingy - I was writing this and it was working when logged and then when I went to the next video and saw
...
I was like what the hell is ellipses doing in there haha! thanks for the tip dude!Steven Parker
231,269 PointsSteven Parker
231,269 PointsIt also doesn't look like you need "store" there.