- use comments as in CSS:
// for single line comments
/* for extended comments over
several lines */
- STATEMENTS - set of instructions, written in correct syntax to be executed
- VARIABLES - though not strictly necessary - declare variabe first then assign value. The example below first declares the variabe and assigns it a value at the same time.
var mood = "happy";
- data type: STRINGS
consist of zero or more characters and are enclosed in quotes (single or double). When using a double quote within a string (while using a double quote for the string itself) - a "\" backslash needs to be put in front of the contained repeated quote (called "escaping"), for example:
var height = "about 5'10\" tall";
- data type: NUMBERS
called 'floating point numbers', can be whole, positive or negative as well as be specified with decimal places:
var temperature = -12;
var temperature = 20.33333;
- data type: BOOLEAN VALUES
Boolean data has 2 values: 'yes' or 'no, 'true' or 'false', 'one' or 'zero' -- these values are NOT enclosed in quotes.
var married = true;
- data type: ARRAY
is a grouping of multiple values under the same name. An Array then gets populated with by adding elements - with the first elements starting at 0 (ZERO!!) - below a long way to populate the array.
var beatles = Array(4);
beatles[0] = "john";
beatles[1] = "paul";
beatles[2] = "george";
beatles[3] = "ringo";
Good practice advised by Jeremy: declare the array and populate it at the same time, always specifying the array (though not necessary - better working method):
var beatles = Array("john", "paul", "george", "ringo");
- operations: ARITHMETIC OPERATORS
+ (plus) for additions
- (minus) for subtractions
/ (forward slash) for divisions
* (asterix) for multiplications
useful shorthand operator: += performing addition and assignment (or concatenation and assignment) at the same time, as shown below:
var year = 2008;
var message = "the year is ";
message += year;
The value of 'message' is now "the year is 2008".
- CONDITIONAL STATEMENTS
scripts will be executed according to a condition met or not. The IF statement will execute its script when the condition is met, the ELSE script will be executed when the condition stated is not met.
NOTE ! the IF statement will only be executed once - for repetitive takss use loops (see below, number 12).
if (1 > 2) {
alert("the world has gone mad!");
} else {
alert ("All is well with the world.");
}
- CONDITIONAL STATEMENTS: COMPARISON OPERATORS
almost exclusively used in conditional statements:
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal
!= inequal
NOTE ! the single equal symbol (=) is used to assign values - not to check for equal values - use 2 equal symbols (==), as shown below:
var my_mood = "happy";
var your_mood = "sad";
if (my_mood == your_mood){
alert("we both feel the same.")
} else {
alert("we're feeling different moods");
}
to check for inequality instead:
var my_mood = "happy";
var your_mood ="sad";
if (my_mood != your_mood){
alert("we're feeling different moods");
}
- CONDITIONAL STATEMENTS: LOGICAL OPERATORS
work on Boolean values, returning a Boolean vlaue of either true or false - operations called operands.
&& and
|| or
! not
Parenthsesis should be used to avoid ambiguities and focus an operator:
if ( !(1 > 2)) {
alert ("All is well with the world.");
}
- LOOPING STATEMENTS: WHILE LOOP
To perform repetitive tasks and execute a script over and over - loops are used which work by executing code within its statemet continuously while the condition is met. When the condition is no longer true - the loop stops.
Similar to the IF statement a WHILE LOOP will check for a condition to be met before executing the code. Different however in its behaviour as it will continue this process while set condition is true, stopping as soon as it's false.
var count = 1;
while (count < 11){
alert (count);
count++;
}
- LOOPING STATEMENTS: DO...WHILE LOOP
As the statement within the curly brackets of the IF statement or the WHILE loop might never get executed (in case the condition is not met) - the DO..WHILE loop is used in case the code should be executed at least once, reversing the order of condition and statement. Below the above example reformatted:
var count = 1;
do {
alert (count);
count++;
} while (count < 11);
Both examples are exactly the same with the same result of a continued code execution until 11 is reached. Varying the code slightly - only the DO...WHILE loop will ensure that the code is at least executed once (while the DO loop would not execute the script at all with the new condition of count < 1):
var count = 1;
do {
alert (count);
count++;
} while (count < 1);
- LOOPING STATEMENTS: FOR LOOP
generally a cleaner way of executing loops - executing the code a specific number of times (basically a reformulation of the DO loop). To take the above example - reformatted as FOR loop:
for (var count = 1; count < 11; count++){
alert(count);
}
Here, the condition gets changed with each loop resulting in the code being executed exactly 10 times.
One of the most common uses of the FOR loop is o act on every element of an array, the number of the elements provided by 'array.length':
var beatles = Array("john", "paul", "george", "ringo");
for (var count = 0; count < beatles.length; count++){
alert(beatles[count]);
}
- FUNCTIONS
To re-use the same code more than once - statements can be wrapped inside FUNCTIONS which in turn can then be called from anywhere in the code.
function shout() {
var beatles = Array("john", "paul", "george", "ringo");
for (var count = 0; count < beatles.length; count++){
alert(beatles[count]);
}
}
Whenever the code should be executed - the FUNCTION can be called by simply writing:
shout();
- FUNCTIONS - ARGUMENTS
Data being passed onto a function is called an ARGUMENT - this is making the function more as useful as it will then act on the Argument.
function multiply(num1,num2){
var total = num1 * num2;
alert(total);
}
The function above can now be passed 2 numbers which will then be multiplied - below the values 10 and 2 are passed onto the function - resulting in the total value of 20:
multiply(10,2);
- FUNCTIONS - RETURN statement
Even more powerful - the result of a function can also be passed back to the statement which invoked the function. The example below creates a function to calculate temperature form Fahrenheit to Celsius - and then assigns the result (which is 35) as data to a variable.
function convertToCelsius(temp){
var result = temp - 32;
result = result / 1.8;
return result;
}
var temp_fahrenheit = 95;
var temp_celsius = convertToCelsius(temp_fahrenheit);
alert(temp_celsius);