<< Click to Display Table of Contents >> Navigation: JavaScript > JavaScript Basics |
The devices can execute any standard JavaScript code that is not related to HTML or browser functions (like document object, location object, etc.). It is possible to use loops (do/while, while, for), conditions (if, switch) and standard JS objects (Array, Date, Math, Number, String). The syntax is very easy to learn and use. A good starting point for learning JavaScript is https://www.w3schools.com/js/
Like many other programming languages, comments start with a double slash "//" //This is a comment Comments can also be spread over several lines: /* is a multiline comment*/ Note that every command in a JavaScript should end with a semicolon ";". |
JavaScript is a weakly typed scripting language which means that no type declaration is needed when creating variables and differently typed variables can be assigned to each other. //To create a variable use the keywork "var": //Creates an integer variable with value 1 var numberVariable = 1; //Creates a string variable with value "myString" var stringVariable = "myString"; // Creates an array with the size of 4 var myArray = new Array(4); Please note that the initialization "var variablename" leads to the deletion of the variable after the script was processed. For JavaScript variables to be used globally, they need to be initialized without the keyword var. It is preferred to use PClient variables to store important data. JavaScript variables should not be used for that. |
The following operators can be used for coding:
|
Loops can be used in JavaScript similar to many other programming languages. WARNING: A loop stops execution of the main application until it is finished. So make sure that only very short loops are used. Three different loop types are available: •The while loop. It only begins if the condition is true and stops as soon as the condition is not true anymore: //Example: loop 10 times: var i = 0; while (i < 10) { i = i + 1; //same as i++; } •The do while loop. This kind of loop will execute the code inside the loop at least once. Only after this the break condition is evaluated: var k = 11; do { k = k + 1; } while (k < 10); //now k is == 12 •The for loop. This loop is used to run the code inside for specified number of times. The for loop statement includes 3 statements. The first is normally used to initialize a (counter) variable. The second statement is the break condition (when the loop will end) and the third is a statement that will be executed after each loop (normally to increment the counter): for (var x = 0; x < 10; x++) { //do something //x will have the values 0..9 } To exit a loop inside a loop, the command break; can be used. To skip the current loop iteration and directly start with the next iteration, the command continue; can be used. |
Two condition statements exist in JavaScript that allow code execution if one or more conditions are true. •The if condition statement. If the condition inside the "if" clause is true, the line behind (or the code block inside the brackets) gets executed. To execute special code if the condition is not true, an "else" clause can be added behind the code of the "if" clause. Both can be combined using else if. var i = 42; if (i < 42) { //execute this code if i is less than 42 } else if (i === 42) { //execute this code if i is equal to 42 } else { //execute this code if none of the above conditions were true (in this case i is greater than 42) } •The switch statement. The switch statement makes it easy to check the value of exactly one variable without the need to write complex if-else statements: var i = 42; switch(i) { case 1: //do something if i is equal to 1; break; case 2: //do something if i is equal to 2; break; case 42: //do something if i is equal to 42; break; case 48: case 50: //do something if i is equal to 48 or 50; break; default: //do something if none of the above cases was true break; }
Note the break; statement in every case. If this is missing, the next case will also be executed even if the case condition is not true (for values 48 and 50 the same code will be executed). It is good practice to have a default case at the end of the switch. This gets executed if no other case matched. |
JavaScript also provides the typeof operator. With this the data type of a variable can be checked: var myNumber = 42; var myString = "Hello World"; print("My number is of type " + (typeof myNumber) + ", my string has type " + (typeof myString)); The return values of typeof are: •boolean •string •number •function •object •undefined |