JavaScript at its core looks a lot like C++ or Java, but is generally simpler.
Special tags and event handlers distinguish JavaScript from the document layout language.
<script>....</script>
tag pairs.<head> |
<body> var thisYear = now.getYear() document.write(thisYear - 1821) |
Amherst College was founded years ago. |
<form> |
<script language="JavaScript" src="tools.js></script> |
<noscript>....</noscript>
tag pairs, a knowledgeable browser will only display them when JavaScript is turned off:
<script language="JavaScript">
|
|
document.write("This statement is designed to generate an error" |
javascript:
in Navigator's location field, a JavaScript Console will open displaying information about any errors that have occurred since startup.
The console can also be used to type in JavaScript to test it.
Safari > Window > Activity
JavaScript provides for comments, but using them has its disadvantages.
/* Anything from slash-star to star-slash, |
<script>...</script>
pairs and try to execute what falls inbetween:
<script language="JavaScript"> |
Note that JavaScript knows enough to ignore the beginning of the HTML comment tag up to end of the line, but needs to be told to ignore the closing tag.
<script language="JavaScript"> |
// You need to have a JavaScript-capable browser to view this page. |
JavaScript has a small but full-featured set of core data types.
JavaScript Data Types |
||
Data Type |
Definition |
Literal Examples |
Null |
A valueless, unitless, dimensionless quantity. |
null |
Boolean |
A logical value. |
true |
Number |
Either an integer or a floating-point number, |
5280
|
String |
A series of characters of arbitrary length, delimited by a matching pair of single or double quotation marks. |
"Amherst College" |
Hexadecimal integer literals can have their X and A-F characters in either upper or lower case.
JavaScript String Special Character Codes |
|||
Definition |
Character Code |
Numeric Code |
Examples |
Horizontal Tab |
\t |
\011 |
document.write("field 1\tfield 2") |
|
|||
Newline |
\n |
\012 |
document.write("line 1\nline 2") |
|
|||
Double Quote |
\" |
\042 |
document.write("A \"strange brew\".") |
|
|||
Single Quote |
\' |
\047 |
document.write('It can\'t be done!') |
|
|||
Backslash |
\\ |
\134 |
document.write("The tab character: \\t") |
|
|||
Any Latin-1 character |
Octal |
\nnn |
document.write( |
|
|||
Any Latin-1 character |
Hexadecimal representation, |
\xnn |
document.write( |
|
|||
Any Unicode character |
Hexadecimal representation, |
\unnnn |
document.write("\u03B1 \u03B2 \u03B3") |
|
Unlike PHP, these string literals work within both single and double strings.
Because HTML uses the double quote for tag values, you'll mostly want to use single quotes to delimit your string literals, and a numeric code for a double quote in a string if it also falls within a tag.
JavaScript statements are simpler...usually.
document.write
above are called statements.7 - 4 |
|
However, when you continue onto another line, JavaScript may need some help to know how to interpret the first line:
7 // could be a stand-alone value |
|
JavaScript variables are very flexible, and can represent all of the above data types.
thisIsAVariable |
Variable names are case sensitive.
var
keyword:var someVariable, anotherVariable |
Declaring a variable with the var
keyword is recommended, as it helps identify the first use of a variable.
Depending on how they are declared, there are some differences in scope (areas of existence) that will be discussed later in the context of functions.
var
keyword is used or not, variables will have an initial value of null
, unless an assignment of some sort takes place.
pieceOfPi = 3.14 // a number A1School = 'Amherst College' // a string |
Assignments can be combined with declarations for a compact, easily identifiable initialization:
var someVariable = aPreviousValue, anotherVariable = -7.2 |
pieceOfPi = 'Apple' // now a string A1School = true // now a boolean |
Because of JavaScript's loose typing, it's important to be able to determine at any time what type of data a variable holds.
The typeof
unary operator will
take any variable (or literal) as an argument and output a string describing its contents:
document.writeln(typeof pieceOfPi) |
|
To work with JavaScript, no special tools are required, just a text editor to write scripts into a .html file (the source view of Dreamweaver can also be used), and a web browser to view the result.