Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Learn the basics of the spread operator (...
), a special syntax JavaScript provides to build, combine, and manipulate arrays quickly and more seamlessly.
Resources
Making a copy of an array with the spread operator
One of the main benefits of copying an array is that you preserve the values of the original array. For example, the original mathStudents
and scienceStudents
arrays remain unchanged (won't be mutated) if you decide to push or pop elements in and out of the copies of those arrays.
const mathStudents = [
'Marty',
'Jennifer',
'Lorraine',
'Goldie'
];
const scienceStudents = [
'Emmett',
'Clara',
'Needles',
'Strickland'
];
const mathStudentsCopy = [...mathStudents];
const scienceStudentsCopy = [...scienceStudents];
// This affects the copied arrays only
// The original arrays remain unchanged
mathStudentsCopy.pop();
scienceStudentsCopy.push('Marvin');
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up