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 The Solution

Heidi Fryzell
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Heidi Fryzell
Front End Web Development Treehouse Moderator 25,178 Points

I 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
Steven Parker
231,269 Points

If 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
Robert Young
Robert Young
11,579 Points

Steven 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!

function addNewTeachers(arr) {
    for (let i = 0; i < arr.length; i++) {
        teachers.push(arr);
        let store = teachers[i]
        console.log(teachers[i]);
    }
    return teachers[i];
}
Steven Parker
Steven Parker
231,269 Points

It also doesn't look like you need "store" there.