comments in codeComments are not shown to the user but they are very useful for the programmer who must maintain or update the code you wrote. Use comments whenever the purpose of the code is not immediately obvious. If you use complex structures or programming tricks, explain them!
For a single comment line we use:
// double slashes - This is a comment line which is not displayed to the user
Over several lines, use a block structure:
/* slash-asterisk to start the block
and end the block with asterisk-slash like this */
- Name of variable must begin with a letter or an underscore ( _)
- Names are case-sensitive - name is not the same variable as Name
- Declaration may be explicit using var:
- Declaration may include initialization:
- Declaration may be implicit, without using var:
- The = operator assigns a value to a variable:
y=5; y takes on value 5
var datej=new Date();
the operatorsWe use the common arithmetic operators, basically the same as Java:
Given that variable y contains 5
Assignment operators assign a value to a variable:
Given that variable x contains 10 and variable y contains 5
With character strings, the + operator is used to do a concatenation:
Relational operators compare variables and values:
Given that variable x contains 5:
Conditional operators are used for multiple compares (and/or):
Given that variable x contains 6 and variable y contains 3:
decision structureIn programming, decision structures are used all the time to control the execution cycle.
instructions run if condition is true;
instructions run if condition is false;
creating a functionA function is a script written in the <head> section.
The function is activated when the page opens. The function can then be called by an event in the <body> of the document.
In this example, the function will execute an alert (a small message window) when the page opens (event is onload).