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 trialavremel
8,448 PointstoString();
var my_array = [2,3,4];
console.log(my_array);
my_array.push(5);
console.log(my_array);
Without the toString() function I still get an output of :
2, 3, 4 and then 2, 3, 4, 5
If so, why do I need the toString() function?
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Avremel,
I have tested myself in both firefox and chrome and have noticed that you get the expected results in both, not the unexpected behavior that Jim shows in the video.
I did some research on it and it appears that it was a bug in older versions of chrome or possibly webkit based browsers in general which would include safari. As far as I know, it had always worked as expected in firefox.
Here's a few stackoverflow questions on this issue if you're interested:
http://stackoverflow.com/questions/4198912/bizarre-console-log-behaviour-in-chrome-developer-tools
An answer in the first one has a link to the webkit bug report: https://bugs.webkit.org/show_bug.cgi?id=35801
I'll have to admit that I don't understand a lot of what's in that link but take a look at comments 31 and 32 on that bug report.
It's my understanding that this was considered a bug and is now fixed.
This is probably something you no longer have to worry about.
You can simply log the array and not have to worry about the toString()
method.
Agapito Cruz
21,486 PointsHello avremel,
Javascript is peforming the type conversion (array to string) for you. It does that a lot when operands or arguments are not the type it expects. It is usually better to be explicit, however, so you can be sure of the outcome.
Hope this helps.
-Agapito
avremel
8,448 PointsThanks for the quick response. I still don't understand why in the video (from 2:30), the teacher's output for the same code was: 2,3,4,5 and then 2,3,4,5. Could it be due to an older version of Chrome?
avremel
8,448 Pointsavremel
8,448 PointsThanks Jason for the in-depth response, that clears things up.
Michael Jurgensen
8,341 PointsMichael Jurgensen
8,341 PointsI just wanted to add that this bug exists in Firefox Developer Edition. However my old version of firefox works fine.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Michael,
That's a bit disappointing. I hadn't really looked into the developer edition yet but had assumed it would essentially be the same browser but perhaps with extra features for developers. Now it looks like there may be differences between the two and we might have to consider them as separate browsers.
I tried the code again in the most recent version of firefox (33.1.1) and it still works as expected but I got the same results as you in the developer edition.
It's not always desirable to use the
.toString()
method to solve this problem. You might have an array like this[1, [2, 3]]
and thetoString
method will show it like this'1,2,3'
which isn't an accurate representation of what the array looks like.I'll see if I can research whether it's been reported as a bug or not.