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 JavaScript Objects Object Basics Set the Value of Object Properties

Samuel Kleos
Samuel Kleos
Front End Web Development Techdegree Student 12,719 Points

Why 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(', ')];

2 Answers

Steven Parker
Steven Parker
229,921 Points

These 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.

7,091 Points

the 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