JavaScript/Lexical Structure

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search
Previous: Bookmarklets Index Next: Reserved Words

Contents

[edit] Case Sensitivity

JavaScript is case sensitive. This means that Hello(); is not the same as HELLO(); or hello();

[edit] Whitespace

Whitespace is extra indents, line breaks, and spaces. Javascript ignores it, and it makes the code easier for people to read.

The following is JavaScript with very little whitespace

function filterEmailKeys(evt){
evt=evt||window.event;
var charCode=evt.charCode||evt.keyCode;
var char=String.fromCharCode(charCode);
if(/[a-zA-Z0-9_\-\.@]/.exec(char))
return true;
return false;
}

The following is the same JavaScript with a typical amount of whitespace

function filterEmailKeys(evt) {
    evt = evt || window.event;
    var charCode = evt.charCode || evt.keyCode;
    var char = String.fromCharCode(charCode);
    if (/[a-zA-Z0-9_\-\.@]/.exec(char)) {
        return true;
    }
    return false;
}

The following is the same JavaScript with a lot of whitespace

function filterEmailKeys(evt)
{
    evt = evt || window.event;
 
    var charCode = evt.charCode || evt.keyCode;
    var char = String.fromCharCode(charCode);
 
    if (/[a-zA-Z0-9_\-\.@]/.exec(char))
    {
        return true;
    }
 
    return false;
}

[edit] Comments

Comments allow you to leave notes in your code to help other people understand it. They also allow you to comment out code that you want to hide from the parser, but you don't want to delete.

Single-line comments

A double slash, //, turns all of the following text on the same line into a comment, that will not be processed by the JavaScript interpreter.

// Shows a welcome message
alert("Hello, World!")

Multi-line comments

Multi-line comments are begun with slash asterisk, /*, and end with the reverse, asterisk slash, */

Here is an example of how to use the different types of commenting techniques.

/* This is a multi-line comment
that contains multiple lines
of commented text */
var a = 1;
/* commented out to perform further testing
a = a + 2;
a = a / (a - 3); // something is wrong here?
*/
alert('a: ' + a);

[edit] Semicolons

In many computer languages semicolons are required at the end of each code statement. In Javascript the use of semicolons is optional, as a new line indicates the end of the statement. This is automatic semicolon insertion and the rules for it are quite complex [1]. Leaving out semicolons and allowing the parser to automatically insert them can create complex problems.

a = b + c
(d + e).print()

The above code is not interpreted as two statements. Because of the parenthesis on the second line, JavaScript interprets the above as if it were

a = b + c(d + e).print();

when instead you required it to be interpreted as

a = b + c;
(d + e).print();

Even though semicolons are optional, it's preferable to end statements with a semicolon to prevent any misunderstandings from taking place.

[edit] Literals

A literal represents a fixed value, as opposed to being a variable.

There are several type of literals available. The most common that comes to mind is the string literal, but there are also integer and floating-point literals, array and boolean literals, as well as object literals too.

Details of these different types are covered in JavaScript/Variables and Types

[edit] Identifiers

An identifier is a name for a piece of data such as a variable, array or function.

[edit] Identifier Rules

  • Letters, dollar signs, underscores and numbers are allowed in identifiers.
  • The first character can NOT be a number.


Examples of legal identifiers:

  • u
  • $hello
  • _Hello
  • hello90

[edit] References

  1. Standard ECMA-262 ECMAScript Language Specification, Chapter 7.9 - Automatic Semicolon Insertion


Previous: Bookmarklets Index Next: Reserved Words
In other languages