Home arrow JavaScript arrow Page 2 - 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 - Break Dancing
(Page 2 of 4 )

If you were to see my enormous body today, you would never know that at one point in my life I was a break dancing master. My slick moves allowed me to defy the laws of gravity. Granted I was like eight years old and fat, but no one could do the worm like me. Word to your mother's uncle.

In fact, it was the Summer of Foot Loose, (well for me anyway; in retrospect the movie was playing on regular television by the time I saw it) and I made my mother tell me on an hourly basis that I did in fact 1) look like Kevin Bacon and 2) dance like Kevin Bacon.

Well, since we are talking about breaks (how's that for a segue), you will note that Javascript has to two types of special statements for you to use inside of loops. They are the Break and the Continue.

Breaking It Down

The Break command is used to exit a loop and continue on to the code after the loop. Let's say that I didn't really want you to know how many beers I drank. I could write a code that would exit out of the loop when the value of my counter got so high it was embarrassing:



<script type="text/javascript">

var i=0

for (i=0;i<=10;i++)


if (i==3){break}

document.write("I drank this many beers: " + i)

document.write("<br />")





This would print out:

  I drank this many beers: 0
  I drank this many beers: 1
  I drank this many beers: 2

The code works by assigning a starting value to the counter variable, then saying that if the value is less than or equal to 10, keep doing the loop, and increment the value of the counter by 1 each time through the loop.

It then gives us a conditional statement, in this case an If statement, that says if the value of our counter is equal to 3, break out of the loop and don't print any more, and skip to the next block of code.

So even though we initially told the loop to run ten times, because our special criteria was met, it broke out of the loop after only three times.

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