Basic Javascript Language
Define and set a variable
- var currentItem; // Declare but don't set value
- var count = 0; //Declare and Initialize to 0
- count = 1; // Assign a new value of 1
- count++; // Assign a new incremented value (2)
Types of Variables
Numbers: integer, real
- var i = 5;
- var r = 6.2298;
String: characters surrounded by double or single quotes
- var str = "in the beginnning";
- var my_txt = "Today is Saturday, February 9";
Boolean: value is true or false
- var my_bool = "false";
- var setting = (a < b);
Logic Statements
Conditional - if and if.. else statements and switch
Conditional statements evaluate a condition (which is found within normal parentheses). If the condition is true, it executes the body of the statement (found within curly brackets). A conditional statement may also have an else section that it is to be executed only if the initial condition is false (the else body statement is also within curly brackets).
DO NOT use x=y for a conditional statement - this means x equals y
DO USE x==y for a conditional statement - this means is x equivalent to y:
if ( men == woman ) {
despair;
} else {
rejoice;
}
or with no Else clause:
if ( grade != passing ) {
despair;
study some more;
}
Switch statement
The switch statement or case statement is like a lot of if elses stacked up. You call it with your variable name as the parameter and then when it executes only the statement that matches the value of your variable is executed. Always provide a case default: to provide a exit when none of the expected values was set.
switch (varname){
case 1:
statements;
break;
case 2:
statements;
break;
case default:
statements;
}
Looping (Repetition) - while, do...while, and for statements
Looping statements are conditional statements that may be executed more than once if the criterion remains true. The while and do...while statements test a particular condition whereas the for statement says at the beginning how many times it will execute. The do...while statement ALWAYS executes at least once since it tests the condition at the finish of the body. The while statement, like the if statement, will not execute even once if the condition is not true the first time through.
while and do...while
initialize; statements;
} |
do {
statements; } while (condition); |
Example - each of the routine below displays three alert dialogs, first with "Sally A", then "Rachel C+", then "Jay B".
var classnames = Array("Sally", "Rachel", "Jay");
var classgrades = Array("A", "C+", "B");
count = 0;
while (count < 3){
alert (classnames[count] + " " + classgrades[count]);
count ++;
} VS: var classnames = Array("Sally", "Rachel", "Jay");
var classgrades = Array("A", "C+", "B");
count = 0;
do {
alert (classnames[count] + " " + classgrades[count]);
count ++;
} while (count < 3)
for
for (initial condition; test condition; alter condition){ statements; }
var classnames = Array("Sally", "Rachel", "Jay");
var classgrades = Array("A", "C+", "B");
for (var count =0; count < 3; count++)
alert (classnames[count] + " " + classgrades[count]);
}
Functions
Create a function
function function_name(arguments) {
statements;
}
Example:
function convertToCelsius(temp){
var result = temp - 32;
result = result / 1.8;
return result;
}
Call (invoke) the function
function_name(arguments);
Example:
var f_temp = 95;
var c_temp = convertToCelsius(f_temp);
alert(c_temp);
which results in an alert dialog with the value of 35
