- Scope and Visibility
-
How to put into HTML page
-
use script tag
-
use comments for dumb browsers
- // is the javascript comment, to prevent it from executing the closing html comment
-
scripts in <head> section
- executed when called
- ensures that it's loaded before it might be needed
-
scripts in <body> section
- executed while the page loads
-
external scripts
- <script type="text/javascript" src="xxx.js"></script>
-
Language
-
Statements
- case sensitive
- separated by semi-colon or blank line
- can be grouped into blocks with { }
-
Comments
- Single line //
- Multi-Line with /* */
-
Variables
- must begin with a letter, or underscore
-
declaration
-
examples
- var x;
- var longNamedVariable;
- var x = 5;
- var longNamed = "Literal in quotes";
-
notes
- variables can be redclared without losing their value
-
Operators
-
Assignment
- =
- +=
- -=
- /=
- *=
- %=
-
Arithmetic
- +
- -
- /
- *
-
%
- modulus
- x=5%2; // means x = 1
- ++
- --
-
String concatenation
-
+
- if you add a number and a string, result is string
-
Comparison
- ==
-
===
- equal, value and type
- !=
- >
- <
- >=
- <=
-
Logical
- &&
- ||
- !
-
Conditional
- the shortcut if
-
variablename=(condition)?value1:value2
- eg
- x=(4<5)?"yes":"no"; // x will be yes
-
Conditional
-
if
- if (condition) statement or block
- if (condition) {} else {}
- if (condition) {} else if (condition2) {} else {}
- switch
-
Functions
- put them in the head
- not executed until called (naturally)
- example call
- procedures can be created by leaving out the return
-
Popup Boxes
-
alert("sometext");
- user must press OK
-
confirm("sometext");
- user is offered OK or Cancel. returns true or false respectively
-
prompt("sometext", "defaultValue");
- user is offered OK or Cancel. returns value or null respectively