Solved

Curious result looping through array

Posted on 2012-04-10
5
245 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

762 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