Curious result looping through array

This may be obvious to the experts, but not to me as I am still "learning" javascript.

When I execute this code:
<script type='text/javascript'>
Seasons1 = ["Winter", "Spring", "Summer", "Autumn"];
Seasons2 = ("Winter", "Spring", "Summer", "Autumn");
for (i in Seasons1) {
  document.write('<br />');
for (i in Seasons2) {
  document.write('<br />');

Open in new window

I get:

Open in new window

What's going on with "Seasons2", and why?
Who is Participating?
basicinstinctConnect With a Mentor Commented:
well the () syntax with the comma separated string literals is actually invalid. You are essentially created four string literals - the js engine will create one after the other like you suppose and the final one will be the result of the evaluation - but it's WRONGto do what you are doing anyway.

square brackets array literal syntax [] it creates an array just like executing "new Array()"
curly braces is object literal syntax {} it creates an object just like executing "new Object()"
quotes is string literal syntax "" it creates a string just like executing "new String"

it's printed out vertically because you are outputting <br/> elements: document.write('<br />');
your syntax is incorrect - the parentheses will execute the statements inside, the result evaluating those statements is being stored in seasons2.

whatever you are trying to do you are not doing it right.
perhaps you mean to do this:

Seasons2 = {0:"Winter", "foo":"Spring", "bar":"Summer", 3:"Autumn"};
ShanghaiDAuthor Commented:
Thanks.  The [ ] syntax in Seasons1 works as I expect/want it to work -- and your suggested { } sytax also gives the same result.

My question was really aimed at trying to understand/learn what the ( ) sytax in Seasons2 is dong.  It now seems clear that it does NOT create an array (as [ ] and { } do).

the parentheses will execute the statements inside, the result evaluating those statements is being stored in seasons2
Does this mean that in executing/evaluating what is inside the partnthese that Seasons2 is intially set to "Winter", then to "Spring", then to "Summer" and finally to "Autumn"  -- each "overwriting" the other?  (That would make sense to me).

I'm still curious why the output is printed vertically -- how/why does document.write() do this?
oops I meant "new String()"
ShanghaiDAuthor Commented:
Thank you for this clear explanation.  I'm always happy to improve my knowledge and understanding.  (I also should have picked up on the document.write('<br />') by usign Firebug to inspect the vertical text.  I will remember to do this next time!)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.