JavaScript: The Definitive Guide

Previous Chapter 2 Next
 

2. Lexical Structure

Contents:
Case Sensitivity
Whitespace and Line Breaks
Optional Semicolons
Comments
Literals
Identifiers
Reserved Words

The lexical structure of a programming language is the set of elementary rules that specify how you write programs in the language. It is the lowest-level syntax of a language, and specifies such things as what variable names look like, what characters are used for comments, and how one program statement is separated from the next. This short chapter explains the lexical structure of JavaScript: it covers the above topics and others.

2.1 Case Sensitivity

JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed "while", and not "While" or "WHILE". Similarly, online, Online, OnLine, and ONLINE are four distinct variable names.

Note that HTML is not case-sensitive, which, because of its close association with JavaScript, can be confusing. In particular, names of event handlers[1] are often typed in mixed-case in HTML (onClick or OnClick, for example) but must be all lowercase when referenced from JavaScript (onclick).

[1] Event handlers are pieces of JavaScript code used as the value of HTML attributes.

Case Sensitivity in Internet Explorer

In Internet Explorer 3.0, the core JavaScript language is case-sensitive, as it is in Navigator. Unfortunately, all of the objects, and their methods and properties, added to this core language by client-side JavaScript are case-insensitive in IE. The Date and Math objects are a built-in part of core JavaScript, so they are case-sensitive in IE; to compute a sine, you must invoke Math.sin(), exactly as shown here. On the other hand, the Document object is part of client-side JavaScript, so it is not case-sensitive. This means that where you would type document.write() in Navigator, you could use Document.Write(), DOCUMENT.WRITE(), or even DoCuMeNt.WrItE() in Internet Explorer.

All user-defined variables, functions, and objects are case-sensitive in IE; it is just the client-side objects and their predefined methods and properties that are not. This does mean, however that you need to be careful how you name your variables and properties. For example, the Window object has a property named parent. In Navigator, it would be perfectly safe to create a new property of a Window object and name it Parent. This would not be okay in IE, however, and would either cause an error or overwrite the value of the parent property.

The reason that client-side objects are not case-sensitive in IE is that IE allows the same client-side objects to be used by the VBScript scripting language. VBScript, like Visual Basic, is not case-sensitive, so Microsoft felt that their client-side objects must not be either. Because of Microsoft's requirement for VBScript, it is not likely that the client-side objects will become case-sensitive in a future version of IE; this is an incompatibility that we will have to live with.

This incompatibility presents a worst-of-both-worlds situation. Because Navigator is case-sensitive, you must be sure to type all your object, method, and property names in exactly the correct case. But because IE is not case-sensitive, you can't take advantage of Navigator's case sensitivity to create different variables with the same spelling and different capitalizations.


Previous Home Next
Exploring JavaScript Book Index Whitespace and Line Breaks

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
This HTML Help has been published using the chm2web software.