Statements G-Z

if and else
Use if when a simple or compound statement must only be executed if certain variables are already set to compatible values. The simplest if statement may be a test for division by zero:
if (x!=0) {y=a/x;}

Use else to catch instances where the preceding if test condition failed. else cannot be used except immediately after the closing } of a valid if statement. In situations where if statements are nested inside other if statements, the else statement will apply to the if statement immediately preceding. Take care to put your else statements beneath the correct } bracket.
if (x!=0) {y=a/x;}

else { alert( "Division by zero attempted!" ); }

Use return to stop execution of the current function and return control to the calling function/procedure.
function my_func(x,y) {
     return (x+y);

var sum = my_func(4,8);
The code has been adapted to run from HTML here
In this example, the alert box should show the result as 12.

Each variable in a function must be declared using var and optionally, var can be used to initialise the variable. Variables defined outside functions can be declared without var.
function my_func(x) {
     var y=4;

Simple loops can use while when the variable is already initialised but the contents of the while loop MUST increment or update the variable(s) in the test condition.

var x=0;
while (x<10) {x++;}

The loop executes repeatedly until the test condition evaluates to false. The test condition is evaluated before each loop is executed. Note the difference between for and while test conditions, for loops execute until the test condition is true. while loops execute until the test condition is false.

This is part of Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.