Javascript Lesson 8

8.1 The For Loop

A repeat loop cycles through a group of statements until a condition is met. For instance, you can use a for loop to cycle through an array and print it to the screen. Or, you might want to look through the Array for some specific information using the if statement.
The for loop is the most common used repeat loop.
This is the syntax of a for loop:

for ( initial expression; condition; update expression ){

The italic text means that the parameter is optional.
The initial expression defines the starting point of loop. It sets the value of a variable to a chosen value. The condition tests the variable for a certain condition and when it is reached, the loop stops and the code after the for loop gets executed. The update expression determines how the variable is increased or decreased.
Below is an example where the for loop is used to print the numbers 1 through 10 on your screen:

<script language="JavaScript">
var x
for(x=1; x<=10; x++){
  document.write(x + "<br>")

This is the result:

You can also declare the variable as part of the initial expression. Here is how that would look like:

<script language="JavaScript">
for(var x=1; x<=10; x++){
  document.write(x + "<br>")

Sometimes you may want to break out of a loop or skip part of a loop. The break statement allows you to get out of a loop if some specific condition occurs. This is done by detecting the condition with the if statement that executes the break statement.
An example of the break statement:

<script language="JavaScript">
var sum = 0;
for(var x=1; x<=10; x++){
  sum += x  // you can also write sum = sum + x
  if(sum <= 15){
     document.write(sum + "<br>")

The script used as example isn't very useful, but the break statement is often used. For instance, in a script to print the prime numbers.

8.2 The While and Do-While Loops

The while loop tests the supplied condition and continues to execute until the condition is met.
This is the syntax for it:

while (condition){

And this is the syntax for the do-while loop.

   } while (condition)

The primary difference between these two loops is that the do-while loop will always execute one time whether the condition is true or not whereas the while loop will only execute if the condition is true.
Be aware that when you are using any of the loops, you can set up a condition that will cause a continuous loop. This will cause your page to hang up. So make sure that the execution of the loop will be stopped by a realistic condition.

8.3 The Noscript Tag

Use the <noscript> </noscript> tag to inform users that their JavaScript is currently turned off. You might also want to provide some information for turning it back on.

8.4 The Navigator Object

The navigator object is used to detect information about the browser a surfer is using. The navigator object has 8 properties which are listed below with the value for the Browser you are using now:





Mime types about which your browser knows.




The appName property contains the official name of the browser application.
The appCodeName property return some more specific information about the browser.
The appVersion and userAgent contain information on the browser's version, platform and the country for which the browser is released.
The language property contains a two letter code to indicate the language version of the browser.
The platform property reflects the operating system.
The mimeTypes and plugings are both array objects.
The mimeTypes contains a listing of files of the type MIME (Multipurpose Internet Mail Extension) that is quite extensive.
The plugins object contains information on the plugins that are available.

The navigator object has 5 methods: JavaEnabled(), preference(), taintEnabled(), plugins.refresh() and savePreferences(). navigator.javaEnabled() will return true or false on whether Java is enabled or not.
navigator.preference() is used to read or write the brower preferences. For instance, you could change the browsers homepage.
taintEnabled() specifies whether data tainting is enabled or not.
plugins.refresh() makes newly installed plug-ins available and optionally reloads open documents that contain plug-ins.
savePreferences() saves the Navigator preferences to the local file prefs.js.

8.5 Exercices

  • Make a script where you print the numbers starting with 10 and ending with 1 to the screen.
  • Write a script that shows the type of browser that is being used along with the platform.
Home | Code | Learn
© 2007-2008 ProgLogic, all rights reserved. | is created by Stijn Strickx. | e-mail