From GIS CS4
Revision as of 20:03, 9 February 2015 by Jennyol (Talk | contribs) (For Loops)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

We are now half way through the quarter and we would like to review the JavaScript programming constructs that we have covered so far.

We will be having a test on Monday 10th Feb.

Code Documentation

It is important that code is well documented. This helps with readability, especially if the code needs to be understood or modified by someone else.

This is a multi-line comment

// This is a single line comment

An Introduction to Data Types

Data values:

  • A literal is an actual fixed value (as you would normally write it) e.g. 75, -30.333, 'z', "This is a text string".
  • A constant uses a symbolic name to represent a fixed value, e.g. #define PI 3.14159265. The symbolic name is used to refer to the value throughout a program. The value remains fixed and may not be altered by the program.
  • A variable uses a storage location in computer memory with an associated symbolic name (identifier) which contains a value that can be read and modified, e.g. var ClassAverage = 95;

Common data types:

  • integer (whole numbers, + or -ve)
  • floating-point (fraction, real or decimal numbers)
  • bool (binary value - 1=true 0=false)
  • char (a single ASCII character)
  • string (a sequence of characters)

ASCII - American Standard Code for Information Interchange A character-encoding scheme originally based on the English alphabet. Encodes 128 specified characters - the numbers 0-9, the letters a-z and A-Z, some basic punctuation symbols, some control codes that originated with Teletype machines, and a blank space - into 7-bit binary integers.

Strong and Weak Typed Languages

Javascript is a weakly-typed, or loosely-typed, or dynamically-typed language. A variable doesn't have to have a type when you declare it or assign to it.

C and C++ are examples of strongly typed languages.


JavaScript is a weakly typed language. There is a single keyword used to declare variables regardless of datatype.

var strMyString      = "Hello"; // This is a string variable
var iMyInteger       = 0;       // This is an integer variable
var bMyBoolean       = true;    // This is a boolean variable
var fMyFloatNumber   = 9.81;    // This is a floating point number

var iLength          = strMyString.length    // get the length of a string
var ch               = strMyString.charAt(3) // get the 4th character of the string (index starts from 0)

var strMyUpperString = strMyString.toUpperCase() // returns the string converted to upper case

A variable or function must be declared before it can be used, otherwise it will generate a syntax error.

A variable should be initialised before it is used otherwise its value may be undefined.

Uninitialised variables are one of the most common sources of bugs in programming.

It is good practice to choose a meaningful name for user defined variables and functions.

It can also be helpful to indicate the datatype of a variable in the name, e.g. using a prefix, such as str for string, i for integer, or b for boolean.



The length of a string is found in the built in property length:

var iStrlen = strTxt.length;

To access the nth character in a string use the method charAt()- returns the character at the specified index (NB index is zero based).

var c10thChar = strTxt.charAt(9);

Summary of JavaScript


Comparison Operators

Arithmetic Operators

If-Else Statements

While Loop

For Loops

For Loop

A for loop consists of three parts separated by ; The first part declares and initialises the loop variable. This is performed at the very beginning before the loop is executed.

The second part is the test that will be performed each time to decide whether the loop will be executed. If the condition is false at the beginning, then the loop will not be executed at all.

The third part is performed after the loop has has been executed and is the code that modifies the loop variable.

var strTest = "Testing, testing 1-2-3!";
var iCount = 0;

// count the number of numeric characters in the string
for (i=0; i<strTest.length; i++)
   var ch = strTest.charAt(i);

   if ((ch >= '0') && (ch <= '9'))



Functions are useful to encapsulate a piece of functionality. This assists the task of program maintenance, as the code is defined in one place rather than many. It also promotes reusability allowing a piece of code to be reused many times within a program, or indeed in another program.

A function parameter may only be referenced within the body of the function. In other words, the scope of a function parameter is local to the function.

// this defines a function called "average" 
// The function has 3 input parameters
// returns the average of 3 integers

var average = function(zi1, zi2, zi3) 
    return (zi1 + zi2 + zi3) / 3;

// the code inside the function will not be run until you
// call the function!  Like this:
var fAverage = average(10, 25, 46);