?
Solved

Curious result looping through array

Posted on 2012-04-10
5
Medium Priority
?
252 Views
Last Modified: 2012-04-10
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(Seasons1[i]);
  document.write('<br />');
}
for (i in Seasons2) {
  document.write(Seasons2[i]);
  document.write('<br />');
}
</script>

Open in new window

I get:
Winter
Spring
Summer
Autumn
A
u
t
u
m
n

Open in new window

What's going on with "Seasons2", and why?
0
Comment
Question by:ShanghaiD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37830766
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"};
0
 

Author Comment

by:ShanghaiD
ID: 37830840
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?
0
 
LVL 23

Accepted Solution

by:
basicinstinct earned 2000 total points
ID: 37830903
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 />');
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37830906
oops I meant "new String()"
0
 

Author Closing Comment

by:ShanghaiD
ID: 37831021
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!)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question