Copy and Combine Arrays with the Spread Operator7:18 with Guil Hernandez
Learn the basics of the spread operator (
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
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');
You need to sign up for Treehouse in order to download course files.Sign up