Table of contents
Buy the book
(Ad, please don’t block.)


Due to its prevalence on the Web and other factors, JavaScript has become hard to avoid. That doesn’t mean that it is well liked, though. With this book, I’m hoping to convince you that, while you do have to accept a fair amount of quirks when using it, JavaScript is a decent language that makes you very productive and can be fun to program in.

Even though I have followed its development since its birth, it took me a long time to warm up to JavaScript. However, when I finally did, it turned out that my prior experience had already prepared me well, because I had worked with Scheme, Java (including GWT), Python, Perl, and Self (all of which have influenced JavaScript).

In 2010, I became aware of Node.js, which gave me hope that I’d eventually be able to use JavaScript on both server and client. As a consequence, I switched to JavaScript as my primary programming language. While learning it, I started writing a book chronicling my discoveries. This is the book you are currently reading. On my blog, I published parts of the book and other material on JavaScript. That helped me in several ways: the positive reaction encouraged me to keep going and made writing this book less lonely; comments to blog posts gave me additional information and tips (as acknowledged everywhere in this book); and it made people aware of my work, which eventually led to O’Reilly publishing this book.

Therefore, this book has been over three years in the making. It has profited from this long gestation period, during which I continually refined its contents. I’m glad that the book is finally finished and hope that people will find it useful for learning JavaScript. O’Reilly has agreed to make it available to be read online, for free, which should help make it accessible to a broad audience.

What You Need to Know About This Book

Is this book for you? The following items can help you determine that:

Who this book is for

This book has been written for programmers, by a programmer. So, in order to understand it, you should already know object-oriented programming, for example, via a mainstream programming language such as Java, PHP, C++, Python, Ruby, Objective-C, C#, or Perl.

Thus, the book’s target audience is programmers who want to learn JavaScript quickly and properly, and JavaScript programmers who want to deepen their skills and/or look up specific topics.

What’s not covered
This book focuses on the JavaScript language proper. For example, you won’t find information on programming web browsers (DOM, asynchronous programming, etc.). However, Chapter 33 points to relevant material.
How this book is organized

This book is divided into four parts, but the main two are:

  • JavaScript Quick Start
  • JavaScript in Depth

These parts are completely independent! You can treat them as if they were separate books: the former is more like a guide, the latter is more like a reference. The Four Parts of This Book tells you more about the structure of this book.

What JavaScript version this book uses
This book teaches ECMAScript 5, the current version of JavaScript that is supported by all modern engines. If you have to work with, say, older web browsers, then Chapter 25 explains what features are exclusive to ECMAScript 5.

Tips for Reading This Book

The most important tip for learning JavaScript is don’t get bogged down by the details. Yes, there are many details when it comes to the language, and this book covers most of them. But there is also a relatively simple and elegant “big picture” that I will point out to you.

The Four Parts of This Book

This book is organized into four parts:

Part I
This part teaches you “Basic JavaScript,” a subset of JavaScript that is as small as possible while still enabling you to be productive. The part stands on its own; it doesn’t depend on other parts and no other parts depend on it.
Part II
This part puts JavaScript in historical and technical context: When, why, and how was it created? How is it related to other programming languages? What were the important steps that got us to where we are today?
Part III
This part is more of a reference: look for a topic that you are interested in, jump in, and explore. Many short examples should prevent things from becoming too dry.
Part IV
This part gives tips for using JavaScript: best practices, advanced techniques, and learning resources. It also describes a few important tools and libraries.

JavaScript Command Lines

While reading this book, you may want to have a command line ready. That allows you to try out code interactively. The most popular choices are:

Node.js comes with an interactive command line. You start it by calling the shell command node.
All major browsers have consoles for entering JavaScript that is evaluated in the context of the current page. Simply search online for the name of your browser and “console.”

Notational Conventions

The following notational conventions are used throughout the book.

Describing syntax

Question marks (?) are used to mark optional parameters. For example:

parseInt(str, radix?)

French quotation marks (guillemets) denote metacode. You can think of such metacode as blanks, to be filled in by actual code. For example:

try {

“White” square brackets mark optional syntactic elements. For example:

break ⟦«label»⟧

In JavaScript comments, I sometimes use backticks to distinguish JavaScript from English:

foo(x, y); // calling function `foo` with parameters `x` and `y`

Referring to methods

I refer to built-in methods via their full path:


For example, Array.prototype.join() refers to the array method join(); that is, JavaScript stores the methods of Array instances in the object Array.prototype. The reason for this is explained in Layer 3: Constructors—Factories for Instances.

Command-line interaction

Whenever I introduce a new concept, I often illustrate it via an interaction in a JavaScript command line. This looks as follows:

> 3 + 4

The text after the greater-than character is the input, typed by a human. Everything else is output by the JavaScript engine. Additionally, I use the method console.log() to print data to the console, especially in (non–command-line) source code:

var x = 3;
console.log(x); // 4

Tips, notes, and warnings


This element signifies a tip or suggestion.


This element signifies a general note.


This element indicates a warning or caution.

Quickly Finding Documentation

While you can obviously use this book as a reference, sometimes looking up information online is quicker. One resource I recommend is the Mozilla Developer Network (MDN). You can search the Web to find documentation on MDN. For example, the following web search finds the documentation for the push() method of arrays:

mdn array push

Safari® Books Online


Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at

Find us on Facebook:

Follow us on Twitter:

Watch us on YouTube:


I would like to thank the following people, all of whom helped make this book possible.

Preparing for JavaScript

The following people laid the foundations for my understanding of JavaScript (in chronological order):

  • Prof. François Bry, Sven Panne, and Tim Geisler (Scheme)
  • Prof. Don Batory (technical writing, programming language design)
  • Prof. Martin Wirsing, Alexander Knapp, Matthias Hölzl, Hubert Baumeister, and various other former colleagues at the Institute for Informatics of the University of Munich (formal methods, various software engineering topics)

Help with JavaScript

Participants of the es-discuss mailing list
Their answers helped me understand the design of JavaScript. I am deeply thankful for their patience and tirelessness. Four people stood out: Brendan Eich, Allen Wirfs-Brock, Mark Miller, and David Herman.
Readers of my blog 2ality
I published bits and pieces of this book on my blog and got an incredible amount of useful feedback. A few names among many: Ben Alman, Brandon Benvie, Mathias Bynens, Andrea Giammarchi, Matthias Reuter, and Rick Waldron.

More sources are acknowledged in the chapters.


I am much obliged to the following people who reviewed this book. They provided important feedback and corrections. In alphabetical order:

  • Mathias Bynens
  • Raymond Camden
  • Cody Lindley
  • Shelley Powers
  • Andreas Schroeder
  • Alex Stangl
  • Béla Varga
  • Edward Yue Shung Wong
Next: I. JavaScript Quick Start