JavaScript is pretty much the "only game in town" when it comes to client-side computation and web page interactivity. Certainly these days it is by far the most widely used tool for such things. This is in sharp contrast to the situation on the server side, where there are many competing alternatives. The notes on this page deal with the core language. A separate page discusses the DOM and client-side JavaScript.

See these sample files for illustrations of many of the concepts summarized here, and more.

Some History

Some High-Level Concepts

Debugging

For debugging JavaScript, make use of whatever debugging facilities are available, such as the Error Console under the Tools menu in Firefox, or the various features provided by the Firebug add-on to Firefox, for example.

Some Lower-Level Language Details

There are two types of comments in JavaScript:

  1. // for single-line comments, or end-of-line comments
  2. /* ... */ for multi-line comments

JavaScript is case-sensitive, but some aspects of (X)HTML and CSS are not, so be especially careful when mixing markup, styles and code.

Each JavaScript statement should be terminated by a semi-colon (;). This is not strictly necessary, provided each statement is on a separate line, but why tempt fate?

Braces (that is, the { and } symbols) group JavaScript statements into a compound statement. However, JavaScript does not have block scope, though it does have function scope.


Here is a table of the primitive data types in JavaScript, which are somewhat like the primitive types in Java or C++, with the notable exception that String is regarded as a primitive type, and there is no "single character" type.

Type Value(s) Wrapper Object Other Values
Number double-precision floating-point Number NaN,Infinity,-Infinity
String single or double quotes String \', \", \\, "", ''
Boolean true, false Boolean 0, null, undefined, "", '',
and NaN are all false
Undefined undefined N/A N/A
Null null N/A N/A

Here is a list of JavaScript operators:

Notes:


Here are the basic type coercion/conversion rules for deciding what type a result will have when values of different types are involved in an expression that produces the result:


JavaScript has selection constructs analogous to those of Java, C and C++, except that the switch-statement permits the use of string values:

if (condition)
  statement

if (condition)
  statement1
else
  statement2

  
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  ...
else
  default_statement
  
switch (expression)
{
  case value1:
    statement1
    break;
  case value2:
    statement2
    break;
  case value3: //Multiple values can cause the same action
  case value4:
    statement_for_either
    break;
  ...
  default:
    default_statement
}

Note that any "statement" in any of the above patterns can be replaced by a compound statement (several statements enclosed in braces).


JavaScript has looping constructs analogous to those of Java, C and C++:

for (initialization; condition; update)
  statement
  
while (condition)
  statement
  
do
  statement
while (condition);

Notes:

  1. Any "statement" in any of the above loop patterns can be replaced by a code block (several statements enclosed in braces).
  2. The break; statement in the body of a loop causes the loop to be terminated at that point and execution to continue with the next statement following that loop.
  3. The continue; statement in the body of a loop causes the current loop iteration to terminate and execution to return to the "top" of the loop and carry on with the next iteration.

JavaScript function syntax looks like this:

function name([par_1, par_2, ..., par_n]) //parameters are optional
{
    statement(s)
    [return value; | return expression;] //return statement optional
}

Notes:


Some built-in JavaScript objects:

All objects in JavaScript are specializations of a root object called Object. A variable of an object data type in JavaScript is analogous to a reference in Java or a pointer in C++. The syntax for access is also similar, as in myObject.property or myObject.method().

See the relevant examples on thesample files page for much more detail on each of these objects.


Some built-in globals, all of which belong to the window object (as do all programmer-defined variables/objects and functions):

undefined
Infinity
NaN

isNaN()
Number()
String()

parseFloat()
parseInt()

alert(message)
confirm(message)
prompt(message[,defaultResponse])

setInterval(codeToExecute,numberOfMilliseconds)
clearInterval(referenceReturnedFrom_setInterval)
setTimeout(codeToExecute,numberOfMilliseconds)
clearTimeout(referenceReturnedFrom_setTimeout)

Additional Notes

  1. JavaScript is dynamically typed (not strongly/statically typed).
  2. All JavaScript numbers are stored internally as 64-bit floating-point values.
  3. Identifier names start with letter and may contain letters, digits, _ or the (not-recommended) $.

Best Practices

  1. Place all JavaScript code in one (or more if necessary) external files, if possible.
  2. Capitalize identifiers consistently. Actually, since JavaScript is case-sensitive, if you don't that's a "gotcha".
  3. Always use var when declaring variables, and put all variables declared in a function at the beginning of the function.
  4. The "define before using" dictum of many programming languages is not strictly necessary, but is still a good practice.
  5. JavaScript is supposed to interpret the end of a line as the end of a statement, but using a semicolon is better practice.
  6. You can break a string across two lines by using a \ just before the return character, though it should be regarded as a "best practice" not to do so, and use the + operator to concatenate the necessary strings instead.

Potential Gotchas

  1. Strings are immutable and may be delimited by single or double quotes.
  2. If a function is called with too many parameters, the extra ones are ignored;
    if called with too few, the missing ones have a value of undefined.
  3. A variable declared without the keyword var is global, even if declared inside a function.
  4. Don't do this: if (date1 == date2) { ... }
    Do this instead: if (date1.getTime() == date2.getTime()) { ... }
  5. There is a global method eval() that takes a string as input and attempts to evaluate it as JavaScript code. Doing this should be avoided as it represents a security problem.