Chapter 1. Introduction and First Looks

JavaScript is one of the most widely used programming languages; it is also one of the most misunderstood. Its growth has exploded in the last few years, and most web sites use it in some form. Its component-based capabilities simplify the creation of increasingly complicated libraries—most providing effects in web pages that previously required the installation of an external application. It can also be tightly integrated with server-side applications that are created with a variety of languages and interface with any number of databases. Yet for all of this, JavaScript is often considered lightweight and unsophisticated—not like a “real” programming language.

In some ways, JavaScript is too easy to use. To its detractors, it lacks discipline; its object-oriented capabilities aren’t really OO; it exists within a simplified environment with only a subset of functionality; it isn’t secure; it’s loosely typed; it doesn’t compile into bytes or bits. I remember reading in a JavaScript introduction years ago that you shouldn’t let the name fool you: JavaScript has little to do with Java. After all, Java is hard to learn.

So what’s the reality? Is JavaScript a fun little scripting language—lightweight, helpful, but not to be taken seriously? Or is it a powerful programming language you can trust with some of your site’s most important functionality? The reality of JavaScript, and hence the confusion, is that it’s two languages in one.

The first is a friendly, easy-to-use scripting language built into web browsers and other applications, offering functions such as form validation and cool stuff like drop-down menus, color fades during data updates, and in-place page edits. Because it’s implemented within a specific environment—usually a web browser of some form—and within a protected environment, JavaScript doesn’t need to have functionality to manage files, memory, or many of the other programming language basic components, making it leaner and simpler. You can begin programming in JS with little or no background, training, or even prior programming experience.

The second language, however, is a mature, full-featured, carefully constrained, object-based language, which does require more in-depth understanding. Used correctly, it can help web applications scale (increase their number of users) with little or no change to the application on the server. It can simplify web-site development and add a level of sophistication, making a good site appear even better to its visitors.

Used incorrectly, JavaScript can also open security holes to your site, especially when used in combination with other functionality, such as a web service or database form. It can also make a page unusable, unreadable, and less accessible.

In Learning JavaScript, I’m going to introduce you to both languages just described: the fun scripting language, as well as the powerful object-oriented programming language. More importantly, I’m going to show you how to use JavaScript correctly.

Twisted History: Specs and Implementations

Learning a programming language doesn’t require learning its history—unless you’re a language like JavaScript, whose history is reflected in web pages today.

JavaScript originated with Netscape, back when it was first developing its LiveConnect server-side development. The company wanted a scripting language that could interface with the server-side components and created one called “LiveScript.” Later, after an initial partnership with Sun, owner of the Java programming language, the Netscape engineers renamed LiveScript to JavaScript, even though there was and is no connection between either programming language. Well-known JavaScript guru Steven Champeon wrote:

Rewind to early 1995. Netscape had just hired Brendan Eich away from MicroUnity Systems Engineering, to take charge of the design and implementation of a new language. Tasked with making Navigator’s newly added Java support more accessible to non-Java programmers, Eich eventually decided that a loosely typed scripting language suited the environment and audience, namely the few thousand web designers and developers who needed to be able to tie into page elements (such as forms, or frames, or images) without a bytecode compiler or knowledge of object-oriented software design.

The language he created was christened “LiveScript,” to reflect its dynamic nature, but was quickly (before the end of the Navigator 2.0 beta cycle) renamed JavaScript, a mistake driven by marketing that would plague web designers for years to come, as they confused the two incessantly on mailing lists and on Usenet. Netscape and Sun jointly announced the new language on December 4, 1995, calling it a “complement” to both HTML and Java.

(From “JavaScript: How Did We Get Here?” O’Reilly Network, April 2001.)

Not to be out-engineered, Microsoft countered Netscape’s effort with the release of Internet Explorer and its own scripting language—VBScript—derived from the company’s popular Visual Basic. Later, it also released its own version of a JavaScript-like language: JScript.

The competition between browsers and languages impacted the early adoption of JavaScript within many companies, especially as the difficult challenge of maintaining cross-browser compatible pages increased—not to mention confusion about the name.

In an effort to cut through the compatibility issues, Netscape submitted the JavaScript specification to the European Computer Manufacturer’s Association (ECMA) International in 1996, to reissue it as a standardized work. Engineers from Sun, Microsoft, Netscape, and other companies holding a stake in the language were invited to participate, and the result was the release of the first ECMAScript specification—ECMA-262—in June 1997. Since that time, most companies that support a version of JavaScript (or JScript or ECMAScript) have agreed to, at a minimum, support ECMA-262.

Tip

You can download a PDF of ECMA-262 at http://www.ecma-international.org/publications/standards/Ecma-262.htm. It’s not exciting reading, but it does make a good companion reference.

The second version of ECMA-262 was strictly a maintenance release. The third, and current, version was released in December 1999.

However, this wouldn’t be JavaScript if the confusion ended with the passing of ECMA-262. Scattered about the Web is discussion of a new version of ECMAScript, designated ECMA-357. However, this isn’t a new edition or version of ECMAScript; it’s an extension known as E4X. The purpose of the extension is to add native XML capability to ECMA-262. ECMA-357 was published in 2004, and at this time, JavaScript 1.6 has partially implemented E4X.

What’s important to remember from all of this is that many of these older versions of scripting langauges are still in use, even today. It’s not uncommon to find old JScript or the earliest versions of JavaScript. To clarify all the versions of scripting languages and how they relate to one another, Table 1-1 provides an approximate correspondence between JavaScript, JScript, and ECMAScript version, and what version of each is supported by today’s most popular web browsers.

Table 1-1. Script support in browsers

BrowserScript supportDocumentation URL
Internet Explorer 6.xECMA-262 (v3) /JScript 5.6 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/1e9b3876-3d38-4fd8-8596-1bbfe2330aa9.asp
Internet Explorer 7.x (Windows XP)ECMA-262 (v3) /JScript 5.6 http://msdn.microsoft.com/ie/
Opera 8 and 8.5ECMA-262 (v3) /JavaScript 1.5 http://www.opera.com/docs/specs/js/ecma/
Firefox 1.5ECMA-262 (v3) with partial support for ECMA-357 (E4X) /JavaScript 1.6JavaScript 1.5 core reference: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference/JavaScript 1.6 core reference: http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6
Safari 2.x on TigerECMA-262 (v3) http://developer.apple.com/documentation/AppleApplications/Conceptual/SafariJSProgTopics/index.html
Camino 1.0ECMA-262 (v3) /JavaScript 1.5 http://www.caminobrowser.org/
Netscape 8.1ECMA-262 (v3) /JavaScript 1.5 http://browser.netscape.com/ns8/
Various wireless device browsersVariesSite that contains reference to several emulators and testing tools: http://www.wirelessdevnet.com/channels/printlinks.phtml?category=4

When you’re visiting web pages and are curious as to how they implement a specific feature, you can usually tell what version of JavaScript they’re using by how they declare the script block. In addition, there are pieces of these old languages that still influence the more modern versions of JS. We’ll look more closely at the script block later in this chapter, and at the influences of older browsers throughout the book, but it’s important to be aware that old versions of JavaScript and its variations still impact today’s applications.

Tip

Throughout the book, I use both JavaScript and JS interchangeably. In addition, unless otherwise noted, examples in this book are based on ECMA-262 and JavaScript 1.5/1.6.

Get Learning JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.