Home arrow JavaScript arrow Page 4 - Javascript: More Loops and Events

Javascript: More Loops and Events

In our last tutorial we covered functions and a portion of loops, leaving off at the While loop. In this tutorial we will continue with the Do While Loop and hopefully work our way through to a taste of JavaScript Events.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
November 14, 2007
  1. · Javascript: More Loops and Events
  2. · Break Dancing
  3. · And So it Continues
  4. · Finishing Loops

print this article

Javascript: More Loops and Events - Finishing Loops
(Page 4 of 4 )


Remember that band? I don't. I mean, what songs did they play? Oh yeah, you are right. Hot Blooded, Head Games, and who can forget Cold as Ice? I guess I can, actually. What did they have, a temperature fetish?

For...In is another way to loop. You use it when you want to loop though the values held in an array. I know we haven't covered arrays yet, but we will. For now just know that arrays are similar to boxes, like variables are. Except that where variables hold one value, arrays hold many values. Sort of like a box full of variables.

You can also use the For...In on variables and objects (we will discuss these later as well). For now though, let's loop through that array.



<script type="text/javascript">

var dogs

var mydogs = new Array()

mydogs[0] = "Linus"

mydogs[1] = "Charlie"

mydogs[2] = "Violet"

mydogs[3] = "Molly Volly"

mydogs[4] = "Sally Anne"

mydogs[5] = "Lucy (RIP)"

mydogs[6] = "Schroeder (Enjoy the new home)"

for (dogs in mydogs)


document.write(mydogs[dogs] + "<br />")





You can tell several things from the above code: 1) I have too many dogs 2) I named them all after Peanuts characters (Molly Volly is Snoopy's tennis instructor) and 3) If I have any say in it (which I don't) my next dog will be named Pigpen.

Aside from that, here is how the program works. We create a variable named dogs, then an array named mydogs. We then assign the array 7 values (0 is the first value in the array). The values are my dogs' names. We then use the For...In statement to loop through the array and finally print out the values. Note that the For...In line is the one that says: for (dogs in mydogs)

That is it for our coverage of loops. In an entirely new series on JavaScript we will discuss Events, which allow the user to trigger certain things within a browser. They help provide a truly interactive experience and are fun to work with. As I sometimes do, I am going to leave you with a table listing the various Events.




Occurs when the loading of an image is interrupted


Occurs when an element loses focus


Occurs when the user changes a field


Occurs when a user's mouse clicks an object


Occurs when user double-clicks an object


Occurs when an error happens while loading a document or an image


Occurs when an element has focus


Occurs when a key on the keyboard is pressed


Occurs when a key on a keyboard is pressed or held down


Occurs when a keyboard key is released


Occurs when a page or an image finishes loading


Occurs when a mouse button is pressed


Occurs when a mouse is moved


Occurs when a mouse if moved off of an element


Occurs when a mouse is moved onto an element


Occurs when a mouse button is released


Occurs when the reset button is clicked


Occurs when a window or frame is resized


Occurs when text is Selected


Occurs when the submit button is clicked


Occurs when the user exits the page

Beautiful ain't it?

Till then...

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials