Introduction to JavaScript

By Herbert J. Bernstein
© Copyright 2004-2005 Herbert J. Bernstein

Javascript is a language created by Netscape to augment web pages. This is a brief introduction to and summary of JavaScript Statements. Some portions of the language have been glossed over or omitted here. There is a strong relationship to the statements in java and the statements in C++.

For more detail on JavaScript syntax, see the Wikipedia JavaScript Article and Mozilla's https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:About.

JavaScript is written as part of the text of an html document using the <script> tag, as in

<script language="JavaScript"  type="text/javascript" >
<!--
  // lines of JavaScript code
  // ...
//-->
</script>
<noscript>
  lines of HTML to display if JavaScript is not supported
</noscript>

In most modern browers, URLs pointing to blocks of javascript code may be incorporated into a web page with a <script> tag with a src attribute.

Tokens

When we write in most languages, we use linear sequences of characters or glyphs, some of which are group together to make the words of the language and some of which are used as punctuation to help us organize those words. The words and punctuation are called "tokens".

In C, C++, java and JavaScript the characters used are drawn from a limited set of characters, that must, at a minimum include representations of

Java and Javascript permit many more characters than this, drawn from a 16-bit Unicode character set. C and C++ tend to work with more limited characters sets, usually drawn from a simple 7-bit character set. There is increasing use of an 8-but multibyte Unicode character set called UTF-8 (see www.unicode.org).

The tokens are organized into keywords, identifiers, constants, strings, operators and other separators. The keywords are the reserved words of the language. The words that are used as keywords or reserved in C, C++, java or JavaScript are: abstract, asm, auto, boolean, break, byte, case, catch, char, class, const, continue, default, delete do, double, else, entry, enum, export, extends, extern, false, final, finally, float, for, friend, function, goto, if, implements, import, in, inline, instanceof, int, interface, long, native, new, null, operator, package, private, protected, public, register, return, short, signed, sizeof, static, strictfp, struct, super, switch, synchronized, template, this, throw, throws, transient, true, try, typedef, typeof, union, unsigned, var, virtual, void, volatile, while, with

Identifiers are sequences of letters, digits and underscores. The first character of an identifier must be a letter or an underscore. Other characters are permitted in identifiers in java and JavaScript (e.g. currency symbols are also treated as letters), but it is unwise to use them for code that is to be portable. The keywords and reserved words should not be used as identifiers.

Statements

Each portion of a JavaScript program consists of a sequence of statements. Almost all valid C and C++ statements are valid Java and JavaScript statements. One major difference is that there is no goto statement in Java or in JavaScript. Most Java statements are valid JavaScript statements, but JavaScript does not have as many types and supports objects without the full panoply classes.

C and C++ have a preprocessor which is not available in Java or JavaScript.

Since JavaScript does not have strong typing, variables are declared with the keyword var instead of with particular types.

The statements in JavaScript may be organized into:

Comments

Comments may be of the form:

     // comment \n
                       (the \n should not be typed.  It means "new line")

or

      /* comment */

The first form allows a trailing comment on a single line, while the second form allows comments that span multiple lines.

Comments may appear anywhere.

Function Declarations

A function is a named set of statements to be executed when that name is "called". When a function is made part of an object it is called a method.

    function identifier ( 
        formal_argument_list ) { 
        function_body }

If a method does not have formal arguments, the identifier is followed by an empty pair of parentheses. This conflicts with current recommended C and C++ usage which would handle this case with identifier ( void ). When there are formal arguments, the formal_argument_list is a comma separated list formal parameters.

 

Executable statements

See Conditionals.html and Loops.html for more on conditionals and loops.

Operators

C, C++ and Java have a rich repertoire of operators. C++ permits the programmer to overload the existing operators. Java does not. The operators on Java are:

Writing into your Web Page

One of the most powerful tools you have in JavaScript is that you can change your web page on the fly with:

document.write(string);

where string is either quoted string or some combination of string concatenated by the "+" operator as in:

document.write("<p>Hi There " + "Bob");

which produces:

You can also have a variable that is a reference to a string as in:

bob = "Robert";
document.write("<p>Hi There " + bob);

which produces:

Communicating with the User

In addition to writing into the web page, you can also communicate with the user with:


Prepared by Herbert J. Bernstein 9 November 2005.

© Copyright 2004 Herbert J. Bernstein. All Rights Reserved.