Solved

Curious result looping through array

Posted on 2012-04-10
5
246 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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now