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 trialDavid Turner
1,061 PointstoString() or not toString()? that is the question!
In this particular question, there is a javascript method called toString. Obviously, this is not that one because you are passing an argument for a separator and toString does not take one. But may I ask why use join() instead of toString()?
1 Answer
andren
28,558 PointsThe main reason is the exact thing you point out yourself, join
allows you to select your own separator which allows you to style how the resulting string looks. If you use the toString
method on the example array in the quiz the resulting string would look like this:
"Hello Sally,Joan,Raphael,Sam"
If you use the join method with the separator from the quiz you get this string:
"Hello Sally, Joan, Raphael, Sam"
As you can see the second string is spaced better than the first one, so even if comma is the separator you want to use the join
method can be useful for improved spacing.
If you are happy with how the string looks by using the toString
method then there is nothing inherently wrong with using it, but the join
method gives you far more control over the separator which is often useful.
David Turner
1,061 PointsDavid Turner
1,061 PointsSure, I was just curious. I like how some quizzes sometimes stretch what you know by forcing you to use external information, that's why I was curious about it.