Preface
JavaScript is the programming language of the Web. The overwhelming majority of modern websites use JavaScript, and all modern web browserson desktops, game consoles, tablets, and smartphonesinclude JavaScript interpreters, making JavaScript the most ubiquitous programming language in history. JavaScript is part of the triad of technologies that all Web developers must learn: HTML to specify the content of web pages, CSS to specify the presentation of those pages, and JavaScript to specify their behavior. Recently, with the advent of Node ( http://nodejs.org ), JavaScript has also become an important programming language for web servers.
This book is an excerpt from the more comprehensive cover the core JavaScript language, starting with fundamental matters of language syntaxtypes, values, variables, operators, statementsand moving on to coverage of JavaScript objects, arrays, functions and classes. These chapters cover the language itself, and are equally relevant to programmers who will use JavaScript in web browsers and programmers who will be using Node on the server-side.
To be useful, every language must have a platform or standard library of functions for performing things like basic input and output. The core JavaScript language defines a minimal API for working with text, arrays, dates, and regular expressions but does not include any input or output functionality. Input and output (as well as more sophisticated features, such as networking, storage, and graphics) are the responsibility of the host environment within which JavaScript is embedded. The most common host environment is a web browser. Chapters cover the web browser host environment and explain how to use client-side JavaScript to create dynamic web pages and web applications.
The number of JavaScript APIs implemented by web browsers has grown explosively in recent years, and it is not possible to cover them all in a book of this size. Chapters , which are also excerpts from The Definitive Guide .)
Although the Node programming environment is becoming more and more important, there is simply not room in this pocket reference to include any information about server-side JavaScript. You can learn more at http://nodejs.org. Similarly, there is no room in the book for an API reference section. Again, I refer you to JavaScript: The Definitive Guide , or to online JavaScript references such as the excellent Mozilla Developer Network at http://developer.mozilla.org/.
The examples in this book can be downloaded from the books web page, which will also include errata if any errors are discovered after publication:
http://shop.oreilly.com/product/0636920011460.do |
In general, you may use the examples in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. We appreciate, but do not require, an attribution like this: From JavaScript Pocket Reference , third edition, by David Flanagan (OReilly). Copyright 2012 David Flanagan, 978-1-449-31685-3. If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .
To comment or ask technical questions about this book, send email to:
This book is also available from the Safari Books Online service. For full digital access to this book and others on similar topics from OReilly and other publishers, visit http://www.safaribooksonline.com/.
Id like to thank my editor, Simon St. Laurent, for challenging me to excerpt The Definitive Guide down to this more manageable size and also the OReilly production staff, who always manage to make my books look great.
Chapter 1. Lexical Structure
JavaScript programs are written using the Unicode character set. Unicode is a superset of ASCII and Latin-1 and supports virtually every written language currently used on the planet.
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and other identifiers must always be typed with a consistent capitalization of letters. The while
keyword, for example, must be typed while, not While or WHILE. Similarly, online
, Online
, OnLine
, and ONLINE
are four distinct variable names.
Comments
JavaScript supports two styles of comments. Any text between a //
and the end of a line is treated as a comment and is ignored by JavaScript. Any text between the characters /*
and */
is also treated as a comment; these comments may span multiple lines but may not be nested. The following lines of code are all legal JavaScript comments:
// This is a single-line comment./* This is also a comment */ // And here is another./* * This is yet another comment. * It has multiple lines. */
Identifiers and Reserved Words
An identifier is simply a name. In JavaScript, identifiers are used to name variables and functions and to provide labels for certain loops in JavaScript code. A JavaScript identifier must begin with a letter, an underscore (_
), or a dollar sign ($
). Subsequent characters can be letters, digits, underscores, or dollar signs.
JavaScript reserves a number of identifiers as the keywords of the language itself. You cannot use these words as identifiers in your programs:
break delete function return typeofcase do if switch varcatch else in this voidcontinue false instanceof throw whiledebugger finally new true withdefault for null try
JavaScript also reserves certain keywords that are not currently used by the language but which might be used in future versions. ECMAScript 5 reserves the following words:
class const enum export extends import super
In addition, the following words, which are legal in ordinary JavaScript code, are reserved in strict mode:
implements let private public yieldinterface package protected static
Strict mode also imposes restrictions on the use of the following identifiers. They are not fully reserved, but they are not allowed as variable, function, or parameter names:
arguments eval
ECMAScript 3 reserved all the keywords of the Java language, and although this has been relaxed in ECMAScript 5, you should still avoid all of these identifiers if you plan to run your code under an ECMAScript 3 implementation of JavaScript: