Nested if statements
Nested if ... else statements
|
Very often several conditional tests may have to be performed, resulting in IF ... ELSE statements being nested one inside another. This can quickly get messy, and its very easy to miss-match a curly brace. Flow charts are a useful paper and pencil technique for sorting out more complex algorithms. |
Javascript template
About the example templates
While the W3C and ECMAscript (Javascript) standards continue to evolve, browser compliance with any particular version, at any given time, is a moving target. All vendors have added their own tweaks (additional features?) to the standards, which by their very definition, are non standard; so should be avoided if the goal is cross browser compatibility.
The examples therefore focus on standard features, ignoring any cross browser differences, except where specifically mentioned.
Object model
The JavaScript object
This page lists the built in objects, and collections which together comprise the JavaScript object. Some browsers may provide additional (non standard) properties or methods that do not form part of the EMCA scripting specification, or the W3C standards, that specify the components of HTML,XHTML,XML, DOM and CSS.
Functions
Javascript Functions
Javascript functions are the basic building blocks which can be combined together to provide more complex functions. Each of the short programs you have written so far, have started with the word function. The process of structured analysis (or top down analysis) is to take a complex problem and break it down into more manageable chunks. These chunks become the basic building blocks of your program. They let you combine to several operations (program statements) to complete some recognisable task.Variables
Defining variables
Variables are used in Javascript to store values in your scripts. Choosing a suitable name for a variable can help in understanding what a script does. Although not strictly necessary, it is good practice to declare variables before using them. You do this using the var statement.
Naming Variables
Javascript is case-sensitive so naming a variable myVar is different to the variable myvar.Comparision operators
JavaScript Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values. They are used to test whether a condition is true or false
Repetition
Repetition, or Loops
There are several ways to execute a statement or block of statements repeatedly. In general, repetitive execution is called looping . It is typically controlled by a test of some variable, the value of which is changed each time the loop is executed. There are several types of loops: for loops, for...in loops, while loops, and do...while loops, each behaves slightly differently and it is up to the programmer to choose the most appropriate
Selection
Javascript selection (or conditional) statements
Selection, or decisions, are made in Javascript using the statement
if(condition) { //is true } else { //is false }
Simple if statements may be written as a single line, but when the true or false portions consist of multiple statements its more likely to be written as Window messages
Alert, prompt, and confirm message boxes (pop up windows)
Use alert, confirm, and prompt message boxes to obtain input from your user. The boxes are methods of the interface window object. Because the window object is at the top of the object hierarchy, you do not actually have to use the full name (for example, "window.alert()") of any of these message boxes, but it is a good idea to do so, because it helps you remember to which object they belong.
Do while loop
Do while loop
This function introduces the Maths object and one of its methods, namely the random() method. This generates random numbers in the range 0 - 1. The program will loop so long as the random number is greater than the limit set. In this example 0.1. Once a number less than 0.1 is generated the program is to stop. Again I have no way of telling how many numbers will be printed. If I use a while loop, then if the first number generated is less than the limit I'll never enter the loop.

