I have been using JavaScript for over 10 years. I've learned everything I know from analysing other people code, reading blog posts and through trial and error. However, I wanted to see just how many embarrassing things there are that I don't know. Therefore, I set out to read a book about JavaScript.

Since time is the only truly limited resource, I started my quest by searching for the most reputable book. Needless to say, there wasn't one that would stand out as the obvious read (tough, The Good Parts book appears almost in every reading list). Therefore, I've asked around at Freenode, ##javascript channel. The result is a review of the 5 books that you might want to add to your reading list.

JavaScript: The Good Parts (2008)

by Douglas Crockford

As the back of the book says, it is an introduction to syntax (explained using railroad diagrams), objects, array, methods, functions and regular expression. It is not a definite guide to building your first library, though it is a good place to start your journey if you haven't touched JavaScript before.

Personally, I found this book a good collection of the common pitfalls, such as this one. Naturally, most developers will learn to avoid and overcome these perils in the course of the trial and error. Nevertheless, for one thing, this book did influence me to completely ditch the use of non-strict comparison operators.

All of that said, the author made several bigoted statements regarding the coding style. (Nothing that you wouldn't expect from the author of JSLint.)

The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling viruses and other security menaces.
The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement.

While reading this book, feel free to skip the section where the JSON parser is analysed (https://github.com/douglascrockford/JSON-js is an up-to-date version of the latter). Furthermore, instead of reading the Chapter 8 (it is essentially a function reference), dedicate the same amount of time to read the relevant sections of the MDN JavaScript reference.

It has been quite a lot of time since this book has been written. There are more books covering the same subjects in just as much depth, some of them even available for free and include interactive examples. In my opinion, the latter are of a great value to an entry level JavaScript user.

Secrets of the JavaScript Ninja (2008)

by John Resig

If you are thinking of writing your own framework (esp. one that supports legacy browsers and primary deals with DOM manipulation), then this book is for you. The book is written by John Resig, among other things, the creator of jQuery. There is a good amount of reading before you reach the chapters about the DOM manipulation. However, the ultimate aim is to train the ninja for the challenges that await in the ferocious browser environment.

Nevertheless, there are a few things that I did not like about this book. First of all, it is very prolix, e.g.

You might have been somewhat surprised, upon turning to this first page of the part of this book dedicated to JavaScript fundamentals, to see that the topic of discussion is to be functions rather than objects.

We'll certainly be paying plenty of attention to objects (particularly in chapter 6), but when it comes down to brass tacks, the main difference between writing JavaScript code like the average Joe (or Jill) and writing it like a JavaScript Ninja, is understand JavaScript as a functional language. The level of the sophistication of all the code that you will ever write in JavaScript hinges upon this realization.

If you're reading this book, you’re not a rank beginner and we're assuming that you know enough object fundamentals to get by for now (and we’ll be taking a look at more advanced object concepts in chapter 6), but really understanding functions in JavaScript is the single most important weapon we can wield. So important, in fact, that this and the following two chapters are going to be devoted to thoroughly understanding functions in JavaScript.

This could have been expressed in a single sentence:

Understanding JavaScript as a functional language is the most important realisation needed to write a sophisticated code. [The following three chapters are dedicated to understanding functions in JavaScript.]

The lack of succinctness is a major drawback. It is 360 pages long; leaving out all the sensei talk will cut it down to at most 200 pages. Furthermore, I would not have dedicated the whole chapter for regular expression and instead suggested to read O’Reilly’s Mastering Regular Expressions. Nevertheless, the introduction is well written. This book is for ninjas, therefore don't be surprised to tackle esoteric issues along the way.

Overall, the content is great. That is to say, the authors dedication to every topic is heartening, e.g. here is my take to summarise Chapter 8 – the Taming threads and timers:

The chapter begins with an introduction to different functions to control browser timers (timeout and interval). It goes on to explain that JavaScript is single-threaded and how does this affect the timer behaviour. This is backed up using test suits from different browsers. Furthermore, it also highlights browser behaviour inconsistency. The author then gives examples of how timers can be used to handle computationally expensive tasks without locking the thread. Finally, everything is summed up with an implementation of a timer control object to keep things tidy.

If it is sounds good, it is because it is good. Definitely worth reading.

Reading this book taught me a lot, though, the ultimate conclusion I've made is that as long as I need to support the legacy browsers, I am using jQuery.

Pro JavaScript Design Patterns (2007)

by Dustin Diaz and Ross Harmes

This is probably the only book that didn't emphasise code testing. Ironically, it is also the only book with a plenty of bugs in the code examples.

// Accessor without function callbacks: returning requested data in accessors. window.API = window.API || {}; API.prototype = (function() { var name = 'Hello world'; // Privileged mutator method. setName: function(newName) { name = newName; return this; }, // Privileged accessor method. getName: function() { return name; } })();

Furthermore, it is kinda annoying that the authors of the book include references to the functions that are explained only later in the book. Arguably, this does not change the essence of the code that the author is analysing. Nevertheless, having a function that does something behind your back while studying some particular pattern makes you feel uncomfortable.

Overall, this book is divided into 2 parts: "Object-Oriented JavaScript" and "Design Patterns." If you have read any of the books I've named in the article, I don't recommend reading the first part at all. The first part describes Interfaces, Encapsulation and Information Hiding, as well as Inheritance. In my opinion, all of the implementations disregard the nature of the JavaScript. Furthermore, the book is written in 2007, therefore some suggestions are outdated with the ES5.

On the other hand, Part II is not bad. The examples seem to be tested and are quite well thought through, e.g. the factory Pattern is illustrated using the XHR request handler and XML reader, which fits together well. It is most likely that you are already familiar with most of the patterns and are using them without even realising. I've personally made a note about every single pattern introduced in this book, whether that's something as basic as a facade, bridge, adapter pattern, the decorator pattern, the observer pattern, the command pattern, the chain of responsibility pattern, etc. Chances are that if these names are meaningless to you, you will learn if not how to code them, at least how to recognise that the implementation has a name.

Eloquent JavaScript (2011)

by Marijn Haverbeke

The first 100 pages is an easy read. It introduces you to the general concepts of programming, goes through every primitive data type, explain the common operations (string concatenation, comparison), JavaScript gotchas (non-strict comparison vs. strict comparison, coercion, etc.), variable scope and closure definition, and loops. Chapter 3 introduces with functions, recursion. Chapter 4 goes into talking about Objects and Arrays. Chapter 5 error handling, etc.

The author has taken a tutorial-style game-like program development approach, therefore you may come across open to debate examples. Personally, I did not like the use of Exceptions to control the control flow of a program.

Most programmers consider exceptions purely an error-handling mechanism. In essence, though, they are just another way of influencing the control flow of a program. For example, they can be used as a kind of break statement in a recursive function. Here is a slightly strange function which determines whether an object, and the objects stored inside it, contain at least seven true values:
var FoundSeven = {}; function hasSevenTruths(object) { var counted = 0; function count(object) { for (var name in object) { if (object[name] === true) { counted++; if (counted == 7) throw FoundSeven; } else if (typeof object[name] == "object") { count(object[name]); } } } try { count(object); return false; } catch (exception) { if (exception != FoundSeven) throw exception; return true; } }

In as many thousands of lines of code I've written, I never needed to resort to an Exception to control the program flow. The point being not that it is incorrect, but that it shouldn't be abused, and whenever it is possible to rewrite the code without much sacrifice, using Exceptions to control the user flow should be avoided. But then, these are the sort of a real world examples that one may come across while reviewing someone else code and it is very thoughtful of the author to include it.

Quite honestly, this is the only book that I did not read cover to cover. I've read the first 20 pages, got bored (it is well written, though very basic stuff) then I got to the Chapter 7, "Searching." The chapter began with the following paragraph and illustration.

The grey lines are roads, and the numbers next to them are the lengths of these roads. Imagine we need a program that finds the shortest route between two points on Hiva Oa. How could we approach that? Think about this for a moment.

No really. Don't just steamroll on to the next paragraph. Try to seriously think of some ways you could do this, and consider the issues you would come up against.

I knew it is not going to be as straightforward as it might look from the first sight. Anyway, the author takes this issue and starts by modelling the input data. The complexity of the code grows with every page, rising new problems from existing implementation. First considering the most primitive approach (calculating all possible routes), then improving the code by rooting out the redundancies one by one. Through this progressive example, the author introduces ES5 array prototype methods such as some, every and any, the use of filter, map and forEach, as well as explain their implementation. It is nothing out of ordinary, though by practically applying every function, it makes the learning into a game-like learning. In fact, the author went as far as explaining the Binary heap implementation that is used to keep track of the shortest available route. In the words of Brendan Eich:

[Eloquent JavaScript is] a concise and balanced mix of principles and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript!

Nonetheless, this book isn't perfect. The section about the regular expressions is somewhat disappointing. It is more verbal than the rest of the book. I by far prefer the introduction written by John Resig.

The Chapter 11 is an introduction to the basics of the HTTP, W3C standards, the structure of URLs, and cross-browser inconsistencies, as well as briefly refer to the concept of the progressive enchainment. In my opinion, it hardly covers the minimum to make it worth including. Although out of the context, Pro CSS for High Traffic Websites (by Antony Kennedy and Inayaili de Leon) is a by far better introduction to the this topic.

The Chapter 12 is talking about DOM. It gives brief introduction to DOM traversing and manipulation, leading to some basic introduction to CSS and how to manipulate DOM element styles. Chapter 13. Talks about events. As usual, the focus is to highlight the differences between IE and other browsers. I did learn something new in this section, such as the way Opera mistakenly can report a wrong key code if shift key is pressed and how to avoid it. There is the bubbling and propagation. It also describes different states of the mouse and keyboard events and when they should be used (such as mousedown, mouse up and click) and gives you a quick view into an event normaliser. Again, it isn't covered as in depth as in John Resig's Secrets of the JavaScript Ninja (2008).

In conclusion, whether you are an entry-level developer (virtually everything in this book is a must-know), or experienced JavaScript developer, reading through this book will not be a bore. As a very least, if you haven't tackled a similar issue to the one described above, you will find Chapter 7 interesting. If you do, you may as well be surprised and tempted to read the rest of the book. Did I mentioned that this book is available for free?

Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript (2012)

by David Herman

This book stands out from the rest as really well written: the overall structure of the book, the structure of every chapter, the structure of every article is great. It is succinct, it doesn't talk about Ninjas, and yet it is not a bore. It is supposedly dedicated to an experienced JavaScript user. Nevertheless, the first chapter goes through the syntax and common gotchas.

Reading this book is a bit like a treasure hunt. Most of the time you will simply be nodding agreeing with everything the author says (except the part promoting omission of the semicolons; what is that all about?), stumbling upon something new. Personally I enjoyed the introduction to Unicode explaining the difference between 16-Bit Code Units and Code Points. That used to be a bit of a mystery to me.

This book goes on explaining how is the scope defined in JavaScript, explaining closures, it warns about the "with" evils. In fact, it feels a bit like Ninja book but in far less words and stripped of all the legacy and Sensei talk.

As every book, this one has its strengths as well as weaknesses. For instance, the section about the difference between function declaration and named function expression goes into telling about horrible bugs that originate from an error in ES3 specification. It gets even more scary when the author mentions that the obsolete object scoping got into ES5 nonconformant environments, but it does not name them, e.g.

var f = function g () { return 17; }; g(); // 17 (in nonconformant environments)

I did not manage to reproduce it in either Gecko-Based Browsers, Webkit or Presto based browsers. IE8 failed. Good to know. It turned out to be an important/ common oversight that I wasn't aware of; possibly leading to issues with inefficient memory usage. Nevertheless, it would have been nicer if the author took time to explicitly name the nonconformant environments for every example.

The example in the next page of the book talks about the behaviour of the block-local function declaration.

function foo () { return 'foo'; } var tar = function () { if (false) { function foo () { return 'bar'; } } console.log( foo() ); }; tar();

I would have guessed that the output will always be 'bar', though it turns out that this behaviour is inconsistent between JavaScript engines. In fact, ECMAScript standards don't even suggest how this should be implemented.

Personally I got bored in the second half of the book. The author started to talk about the difference between Array and Array-like objects, overloading, dangers of coercion, method chaining, the asynchronous nature of JavaScript, etc. All of this is very important, but it is hard to say anything new about it. If you have already read Resig's Secrets of the JavaScript Ninja or Haverbeke's Eloquent JavaScript, then you might as well limit yourself to glancing through the end of the book.

What is the best book for learning JavaScript?

If you have skipped to the end of the article to find one book, then I will have to disappoint you. Every book has its own approach, some serve as a reference (The Good Parts), some tackle existing and well known every day issues (Ninja), some define a problem and analyse it (Eloquent), while some go point by point exploring every curiosity of the language (Effective JavaScript). The book about design patterns stands for what it is.

Nevertheless, if you had to pick to read either Effective JavaScript, Eloquent JavaScript or The Good Parts, I would suggest the Eloquent JavaScript over The Good Parts. For one thing, Eloquent JavaScript is free and it is just as well written. If you are willing to invest a few bucks, I recommend Effective JavaScript over the Eloquent. After all, Effective JavaScript struck me as one of the best structured books. I wouldn't categorise Ninja book or the Pro JavaScript Design Patterns together with either of the other books; if you have time, read both of them.