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 Arrays Loop Through Arrays Search for a Value in an Array

What is the functional difference between Array.prototype.join() and Array.prototype.toString() ?

inStock.join(", ") does make a comma and a space between array elements while inStock.toString(", ") ignores the space and only has a comma between array elements.

Is there any other reason to use join instead of toString?

Reggie Williams
Reggie Williams
Treehouse Teacher

Hey Kylie Soderblom the main difference is that the join method takes a seperator parameter while toString doesn't take any. Without the parameter given, their behavior is identical

1 Answer

Hi Kylie!

The biggest difference is that toString() just builds a string from an array with elements separated by only a single comma (and no spaces). In other words, there are no options.

With join(), you can specify ANY separator of your choice.

Keep in mind, however, join() and toString() would give the same result (when join is called without an argument).

So this:

var cars = ["Saab", "Volvo", "BMW"];
console.log( cars.join("---") );

Would log the string:

 Saab---Volvo---BMW

to the console.

More info:

https://www.includehelp.com/code-snippets/join-vs-toString-methods-in-javascript.aspx

I hope that helps.

Stay safe and happy coding!