Solved

Curious result looping through array

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem in javascript 17 57
REST call Failing 1 39
Why does my submit button remain disabled 3 34
How to send a javascript array via AJAX (jquery)? 3 41
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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 …
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…

734 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