JavaScript: The Good Parts

The Bottom Line

I just finished reading JavaScript: The Good Parts from O'Reilly Media/Yahoo Press. Getting straight to the point, I would give it 5/5 stars. The main reasons for the rating are that it is well written and organized and full of valuable information and examples. Even though the book is a little dated now, multiple people have recommended that I read it over the past few years. I believe it has held up very well since 2008 when it was published. I think it is a must read for anyone programming with JavaScript, which is basically anyone who has ever had to do any front end programming these days. It is aimed at those who have some experience with JavaScript.


Overall Overview

The book is organized into logical topical sections, starting with an introductory chapter on JavaScript and why only "the good parts". Subsequent chapters delve into syntax and each of JavaScript's major semantic elements. I found the writing style to be easy to read and without noticeable grammatical errors.

Each chapter has diagrams or code examples with extensive commentary for explanation. They chapters about grammar and syntax have numerous railroad diagrams, which are a quick way to describe legal syntax. The chapters about language semantics are filled with commented code samples and full "programs". The code samples build on each other throughout the chapter leaving the reader with a common thread on which to build.

The book is dense. I will definitely need to refer back to it and read the whole book or relevant sections again.

The book has one of the best explanations of closure that I have ever seen. Not only does Crockford explain what closure is but also why you would want to use it. The "why" is really the key for me because explaining what closure is really isn't all that helpful without understanding why. Closure is important because of the way JavaScript was designed, with global variables. Closure provides a way to make variables private.

To Read or Not to Read

If you are looking for a Read/no-Read decision. Here is the bottom line:

  • I liked the thorough explanations and examples.

If you are brand new to JavaScript, this book may be a bit over your head. If you have a good understanding of syntax and basic building blocks of JavaScript, this book is essential reading for how to write good JavaScript.

If you want chapter by chapter summary, thoughts, and errors keep reading, otherwise you can stop here.

Chapter Overviews, Thoughts

The following sections are organized by chapter and include a basic summary of the content and thoughts on the content. The thoughts are in a bulleted list under the summary.

Preface
The preface covers basic info about the book, its goals, and the conventions used in the book.

  • It is slightly confusing who the book is aimed at: "It is intended for programmers who ... are venturing into JavaScript for the first time." then later "This is not a book for beginners." I think the author is saying that it is not a book for learning programming but it's alright if you don't know JavaScript. (p. xi)

Chapter 1: Good Parts
The first chapter is an introduction to the author's motivation to write the book, all languages have good parts and bad parts. Focusing on the good parts makes writing, reading, and maintaining programs much easier. JavaScript is important because it is the language of the browser and can be easily tested in any browser.

Chapter 2: Grammar
The second chapter is full of railroad diagrams describing the JavaScript grammar. There is enough commentary to go along with the diagrams so you can probably get away with reading one or the other.

  • I found the railroad diagrams helpful, but on their own without any commentary it takes quite a bit of time to understand. The author did a good job of walking the reader through each diagram so that it is not up to the reader to digest and understand the diagram.

Chapter 3: Objects
This chapter is dedicated entirely to objects. Nearly every type in JavaScript is an object, including numbers, strings, booleans, null, and undefined. The author explains the way to define, access, and modify objects. There are lots of important pieces of information about objects, objects are always passed by reference, objects have a prototype (even literals), and objects can be used to limit the dangers of global variables.

Chapter 4: Functions
This chapter contains a detailed discussion of functions. It shouldn't be a surprise by now that functions are also objects in JS. Several patterns for using functions are discussed, method invocation, function invocation, constructor invocation, and apply invocation. JavaScript provides an arguments array which allows functions to be written with an unspecified number of parameters. The base objects in JS can be augmented using functions on the prototype. Recursion is discussed and it is noted that JS does not support tail recursion optimization, which means that deeply recursive functions may exhaust the return stack.

A discussion of scope leads to deeper subjects such as closure. Closure allows you to get around the global variable scoping in JavaScript and make properties truly private. This leads into callbacks and modules. Modules are a combination of functions and closure that presents an interface while hiding the state and implementation. Returning the object itself form a function is also covered, also known as cascades. This is probably best seen in libraries like jQuery where you can string methods together that all operate on the same object. Finally currying and memoization are covered.

  • This is probably the longest and densest chapter in the book. Some very important and fundamental concepts are discussed at length. The full extent of the topics is not spelled out but these concepts are what makes JavaScript such a versatile and powerful language.

Chapter 5: Inheritance
Inheritance is different in JavaScript than in many other programming languages. It does provide a pseudo-classical method of inheritance to make things easier for those used to other languages. Creating objects can be simplified by using object specifiers. Prototypal and functional inheritance are discussed and finally parts. You can add functionality to any object by adding methods to it.

  • Again, this chapter seems straightforward on the surface but covers some deep topics that make JavaScript so powerful.

Chapter 6: Arrays
Arrays in JavaScript are not arrays like other programming languages but objects instead. The methods available to the array object are covered in this chapter as well as when to use an array and when to use an object.

Chapter 7: Regular Expressions
This chapter covers regular expressions in JavaScript, which were modeled on Perl. Railroad diagrams make another appearance in this chapter to show regular expression syntax and help illustrate the example regular expressions.

Chapter 8: Methods
This chapter contains a lot of good information on the methods that are available in JavaScript. There isn't anything particularly deep in this chapter, it is more of an API reference.

Chapter 9: Style
This chapter discusses how to write programs in a style that is easy to understand and maintain in the future. Again, this chapter isn't very deep but does have useful information.

Chapter 10: Beautiful Features
This chapter is more or less a summary goal of the book. It describes why you might be interested in eliminating features from a programming language.

Appendix A: Awful Parts
This appendix covers bad parts of JavaScript that you won't be able to easily avoid. The topics include global variables, scope, semicolon insertion, reserved words, unicode, typeof, parseInt, +, floating point, NaN, phony arrays, falsy values, hasOwnProperty, and object.

Appendix B: Bad Parts
This appendix covers bad parts of JavaScript that you can easily avoid. Eliminating these parts will make your programs more reliable and easier to read and maintain. The topics include ==, with statement, eval, continue statement, switch fall through, block-less statements, ++ and --, bitwise operators, the function statement vs the function expression, typed wrappers, new, and void.

Appendix C: JSLint
This appendix covers the authors JSLint implementation, how to use it and options.

  • Running your JS code through JSLint will check for errors and good style. It will help you to eliminate the bad parts of JS and stick to the good parts. It will not ensure that your program works.

Appendix D: Syntax Diagrams

This appendix is just a collection of railroad diagrams on JavaScript syntax.

Appendix E: JSON

This appendix covers JavaScript Object Notation (JSON). This is a data interchange format based on JavaScript's object literal notation.

  • JSON is fast becoming the data interchange format of choice among all types of programs. If you aren't familiar with it, I recommend that you start to learn it.

Final Thoughts

To summarize, the book was a worthwhile read for anyone programming in JavaScript. I am a fairly experienced JavaScript programmer and I found the book to be useful, some parts were a little boring and some parts were very helpful. I believe it would be helpful for beginning JavaScript programmers as well. Advanced JS programmers probably have already learned these concepts but it might be a good refresher.


Loading Conversation