Introduction to Javascript


Since all of the scripts are written using Javascript in todo4teams, here is a brief introduction to this programming language.

Declaration of Variables

With the declaration a variable in the current scope is created, it can be a global variable or local function variable. It can be installed either without value:

var alter;

Or the initial value can be specified equal to:

var alter=32;

Instead of the 32 any expression can be used. The scheme is:

var Bezeichner = Ausdruck;

Control Structures: Branching and Looping

Control structures are instructions, which in itself contain further blocks of instructions. This includes the conditional command, the so called if-command.

if (alter >= 18) {
  println("Volljährig!");
} else {
  println("Noch nicht volljährig.");
}

Between the parentheses is one single expression and between the braces there are one or more commands.

With the switch-command you can make notes to branches, in which a value is compared with many other values ​​and a corresponding command is executed.

Loops

Loops can be expressed in Javascript in various ways:

The while-loop

while (bedingung) {
        anweisungen;
}

or the for-loop:

for (startausdruck; bedingung; iterationsausdruck) {
        anweisungen;
}

Functions

Functions are full-fledged objects in Javascript. They have properties and methods, can be edited or be overwritten, be given as arguments to functions or are generated and returned by them.

Not every argument of a function must be specified in the command, for missing arguments the value is set to undefined. There are different ways to create a function in Javascript. The easiest way is to declare:

function a (Parameter1, Parameter2, Parameter3) {
        anweisungen;
       return ausdruck;
}

Operators

Here is an overview of the operators in Javascript:

OperatorOperand typeOperation performed
*NumbersMultiplication
/NumbersDivision
+Numbers or stringsAddition or string-chain
NumbersSubtraction
%NumbersModulo-operator
<Numbers or stringsSmaller than
<=Numbers or stringsSmaller or equal
>Numbers or stringsBigger than
>=Numbers or stringsBigger or equal
 Any variableAllocation
*=Number-variableAllocation with multiplication
/=Number-variableAllocation with division
%=Number-variableAllocation with modulo-operator
+=Number- or string-variableAllocation with addition or string-chain
–=Number-variableAllocation with subtraction
<<=Integer-variableAllocation with  left-shift
>>>=Integer-variableAllocation with right-shift or zero-filling
>>=Integer-variableAllocation with right-shift
=Elementary typesEqual; identical value
Elementary typesNot Equal; non-identical value
&IntegerBitwise AND
|IntegerBitwise OR
.Object, propertyAccess to property
[ ]Array, integer or stringAccess to array-element
( )Function, any type of parameterFunction command
++Number-variableIncrement
––Number-variableDecrement
&&Boolean valuesLogical AND
||Boolean valuesLogical OR
^IntegerBitwise XOR
~IntegerBitwise NOT (bitwise complementary)
>>>IntegerShift to the right with zero-filling
>>IntegerLeft-shift
<<IntegerRight-shift

Arrays

There are two ways in Javascript to create Arrays:

Generation by Literal Construction

The data-type must not be named. The Javascript-interpreter knows that the square brackets will create an array:

 farben = ["rot", "grün", "blau", "gelb", "grau"];
Generation by Array-Construktor

The difference with the first method is insignificant. Parentheses replace the square brackets and the array-constructor is explicitly listed.

 farben = new Array("rot", "grün", "blau", "gelb", "grau");
Access to Array-Elements

A script can create an array as an empty array that will be filled later by the program with data.

var farben = new Array();
or
 farben = [];

Access to the elements of the array takes part by the array name and an index. The following declarations initialize the array.
farben[0]  = "rot";
farben[1]  = "grün";
farben[2]  = "blau";

Assigning a new value to an array changes elements:
farben[0]  = "orange";

Determining the Size of the Array

The size of the array automatically evolves by the number of occupied elements. For numeric arrays the length property finds out how many elements the array contains: array.length.

array.length is always one more than the highest index. The safest way to include a new element in an array is:
var farben = ["rot", "grün", "blau"];
farben[farben.length] = "gelb";

Objects

Each object (a literal object created) inherits from the prototype of the global object constructor.

Predefined Objects

Javascript knows various implementeted objects:

  • The nameless global object that contains all the variables and objects.
  • Object as a general prototype of which all objects are derived.
  • Function as a prototype for functions
  • Array as a prototype for arrays
  • String as a prototype for character strings
  • Boolean as a prototype for Boolean variables
  • Number as a prototype for numbers (64-bit floating point numbers according to IEEE 754)
  • Math provides constants and methods for mathematical operations. Math can not be used as a constructor.
  • Date for operations on data or points in time and date formats
  • RegExp for regular expressions

In the server-sided Javascript used here also all objects can be accessed that are passed to the script in the context.

Access to Objectproperties and -methods

Properties of objects (methods are also properties) are addressed as follows:

Dot notation:
objekt.eigenschaft;
objekt.methode([Parameter]);

In addition to the dot notation there is the bracket notation, which is far less common.

Error Handling

The command try … catch … finally catches exceptions that occur because of an error or a throw-command. The syntax is as follows:

try {
       // Anweisungen, in denen Ausnahmen auftreten oder ausgelöst werden können
} catch (exception) {
       // Anweisungsfolge, die im Ausnahmefall ausgeführt wird.
       // In diesem Teil kann die Fehlerbehandlung erfolgen.
} finally {
       // Anweisungsfolge, die anschließend in jedem Fall ausgeführt wird.
}

throw("sample exception"); 

At the start the commands in the try-block are executed. If an exception occurs, the control flow is diverted immediately to the catch-block with the exception-object as a parameter.

Normally the exception-block is skipped. After the execution of the try-block (even partially) and of the catch-block the commands in the finally-block are always executed. The finally-part can be omitted or alternatively the catch-part.