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.
Is this book for you? The following items can help you determine that:
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.
This book is divided into four parts, but the main two are:
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.
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.
This book is organized into four parts:
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
.
The following notational conventions are used throughout the book.
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
{
«
try_statements
»
}
“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`
I refer to built-in methods via their full path:
«
Constructor
»
.
prototype
.
«
methodName
»
()
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.
Whenever I introduce a new concept, I often illustrate it via an interaction in a JavaScript command line. This looks as follows:
> 3 + 4 7
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
;
x
++
;
console
.
log
(
x
);
// 4
This element signifies a tip or suggestion.
This element signifies a general note.
This element indicates a warning or caution.
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 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.
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 http://oreil.ly/speaking-js.
To comment or ask technical questions about this book, send email to bookquestions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
I would like to thank the following people, all of whom helped make this book possible.
The following people laid the foundations for my understanding of JavaScript (in chronological order):
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: