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 trialSamuel Kleos
Front End Web Development Techdegree Student 13,307 PointsWhy use the spread operator when you can use join()?
Arent these the same thing?
Spread Operator: let new_array = [ ...old_array ];
Join Method: let new_array = [ old_array.join(', ')];
4 Answers
Steven Parker
231,236 PointsThese lines produce different results. In the first example, the spread operator will create a copy of the old array. But in the second example, the join method will create an array with just one element, and that element will be a string containing a comma-separated list of the old array's values.
josephweiss2
7,094 PointsYes if you use .join() it will return a string
josephweiss2
7,094 Pointsthe array.join()
function returns the array in to a string to be readable on witch way you want(like you putted in a ", "), so when you want to display the array then you might use join, but here your just structuring the array so should not use join because you might use the array on a different way and then the join will disturb you to return a functional return because its built with the commas and spaces extra then a normal raw array.
I hope this helps you
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 PointsSo if I use spread I get returned an array, however if I use join() method then I get returned String. Is that correct? Therefore those two differ by return value. Do you agree? BTW, are objects always mutable? In the video person.name could be changed despite using "const". How come?
Steven Parker
231,236 PointsYour understanding of the return values seems correct. To deepen your understanding, you might try some experiments and see what you get back for yourself.
And a const array cannot be overwritten with another value, but it's content can still be changed. If you want to prevent any changes to an array object, you can call Object.freeze(your_array)
on it.