loading...

JavaScript – Introduction to JavaScript

JavaScript is the programming language of the Web. The overwhelming
majority of modern websites use JavaScript, and all modern web browsers—on
desktops, game consoles, tablets, and smart phones—include 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 web pages, and JavaScript to specify the
behavior of web pages. This book will help you master the language.

If you are already familiar with other programming languages, it may
help you to know that JavaScript is a high-level, dynamic, untyped
interpreted programming language that is well-suited to object-oriented
and functional programming styles. JavaScript derives its syntax from
Java, its first-class functions from Scheme, and its prototype-based
inheritance from Self. But you do not need to know any of those languages,
or be familiar with those terms, to use this book and learn
JavaScript.

The name “JavaScript” is actually somewhat misleading. Except for a
superficial syntactic resemblance, JavaScript is completely different from
the Java programming language. And JavaScript has long since outgrown its
scripting-language roots to become a robust and efficient general-purpose
language. The latest version of the language (see the sidebar) defines new
features for serious large-scale software development.

JavaScript: Names and Versions

JavaScript was created at Netscape in the early days of the Web,
and technically, “JavaScript” is a trademark licensed from Sun
Microsystems (now Oracle) used to describe Netscape’s (now Mozilla’s)
implementation of the language. Netscape submitted the language for
standardization to ECMA—the European Computer Manufacturer’s
Association—and because of trademark issues, the standardized version of
the language was stuck with the awkward name “ECMAScript.” For the same
trademark reasons, Microsoft’s version of the language is formally known
as “JScript.” In practice, just about everyone calls the language
JavaScript. This book uses the name “ECMAScript” only to refer to the
language standard.

For the last decade, all web browsers have implemented version 3
of the ECMAScript standard and there has really been no need to think
about version numbers: the language standard was stable and browser
implementations of the language were, for the most part, interoperable.
Recently, an important new version of the language has been defined as
ECMAScript version 5 and, at the time of this writing, browsers are
beginning to implement it. This book covers all the new features of
ECMAScript 5 as well as all the long-standing features of ECMAScript 3.
You’ll sometimes see these language versions abbreviated as ES3 and ES5,
just as you’ll sometimes see the name JavaScript abbreviated as
JS.

When we’re speaking of the language itself, the only version
numbers that are relevant are ECMAScript versions 3 or 5. (Version 4 of
ECMAScript was under development for years, but proved to be too
ambitious and was never released.) Sometimes, however, you’ll also see a
JavaScript version number, such as JavaScript 1.5 or JavaScript 1.8.
These are Mozilla’s version numbers: version 1.5 is basically ECMAScript
3, and later versions include nonstandard language extensions (see Chapter 11). Finally, there are also version numbers
attached to particular JavaScript interpreters or “engines.” Google
calls its JavaScript interpreter V8, for example, and at the time of
this writing the current version of the V8 engine is 3.0.

To be useful, every language must have a platform or standard
library or API 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. Usually that host environment is a web browser (though we’ll see
two uses of JavaScript without a web browser in Chapter 12). Part I of this book covers the
language itself and its minimal built-in API. Part II
explains how JavaScript is used in web browsers and covers the sprawling
browser-based APIs loosely known as “client-side JavaScript.”

Part III is the reference section for the core API.
You can read about the JavaScript array manipulation API by looking up
“Array” in this part of the book, for example. Part IV is
the reference section for client-side JavaScript. You might look up
“Canvas” in this part of the book to read about the graphics API defined
by the HTML5 <canvas> element, for example.

This book covers low-level fundamentals first, and then builds on
those to more advanced and
higher-level abstractions. The chapters are intended to be read more or
less in order. But learning a new programming language is never a linear
process, and describing a language is not linear either: each language
feature is related to other features and this book is full of
cross-references—sometimes backward and sometimes forward to material you
have not yet read. This chapter makes a quick first pass through the core
language and the client-side API, introducing key features that will make
it easier to understand the in-depth treatment in the chapters that
follow.

Exploring JavaScript

When learning a new programming language, it’s important to try
the examples in the book, and then modify them and try them again to
test your understanding of the language. To do that, you need a
JavaScript interpreter. Fortunately, every web browser includes a
JavaScript interpreter, and if you’re reading this book, you probably
already have more than one web browser installed on your
computer.

We’ll see later on in this chapter that you can embed JavaScript
code within <script> tags in
HTML files, and when the browser loads the file, it will execute the
code. Fortunately, however, you don’t have to do that every time you
want to try out simple snippets of JavaScript code. Spurred on by the
powerful and innovative Firebug extension for Firefox (pictured in Figure 1-1 and available for download from http://getfirebug.com/), today’s web browsers all include
web developer tools that are indispensable for debugging, experimenting,
and learning. You can usually find these tools in the Tools menu of the
browser under names like “Developer Tools” or “Web Console.”
(Firefox 4 includes a built-in
“Web Console,” but at the time of this writing, the Firebug extension is
better.) Often, you can call up a console with a keystroke like F12 or
Ctrl-Shift-J. These console tools often appear as panes at the top or
bottom of the browser window, but some allow you to open them as
separate windows (as pictured in Figure 1-1), which
is often quite convenient.

A typical “developer tools” pane or window includes multiple tabs
that allow you to inspect things like HTML document structure, CSS
styles, network requests, and so on. One of the tabs is a “JavaScript
console” that allows you to type in lines of JavaScript code and try
them out. This is a particularly easy way to play around with
JavaScript, and I recommend that you use it as you read this
book.

There is a simple console API that is portably implemented by
modern browsers. You can use the function console.log() to display text on the console.
This is often surprisingly helpful while debugging, and some of the
examples in this book (even in the core language section) use console.log() to perform simple output. A
similar but more intrusive way to display output or debugging messages
is by passing a string of text to the alert() function, which displays it in a modal
dialog box.

Figure 1-1. The Firebug debugging console for Firefox

Comments are closed.

loading...