Skip to main content
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Adam Tuttle
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Adam Tuttle ( @adamtuttle )

Eloquent Javascript: A Modern Introduction To Programming By Marijn Haverbeke

By on

As the title of the book indicates, Eloquent Javascript by Marijn Haverbeke is not so much about Javascript as it is about computer programming in the context of Javascript. And furthermore, the Javascript used in the book is completely outside the browser and the DOM (Document Object Model); aside from two short chapters at the end, the book focuses primarily on programming technique, syntax, and algorithms and almost not at all on DOM manipulation or client-server communication. This approach, perhaps suspect to those in the web-development world, ends up creating a very informative, very insightful exploration of the Javascript language and of programming theory.


 
 
 

 
Eloquent Javascript: A Modern Introcution to Programming by Marijn Haverbeke, review by Ben Nadel.  
 
 
 

Once upon a time, I used to be a TA (Teaching Assistant) for a web development class at University. When I met with my students in the computer lab, I quickly found that there were two kinds of computer science students: those that could understand the concept of a "variable" and those that could not. No matter how I tried to explain variables, it seemed that some students simply could not wrap their minds around it.

I always felt that their failure of understanding was my failure as a teacher. As such, I was tremendously excited when I read Haverbeke's explanation of variables in programming (pg 16):

You should imagine variables as tentacles, rather than boxes. They do not contain values; they grasp them - two variables can refer to the same value. Only the values that the program still has a hold on can be accessed by it. When you need to remember something, you grow a tentacle to hold on to it, or you reattach one of your existing tentacles to a new value.

This visual, sensory-rich description just felt right! Upon reading this, I suddenly wanted to go back in time to bring this kind of insight to the computer lab at University.

More than just variables, however, Haverbeke consistently demonstrates a mastery of both Javascript and of teaching throughout the book. While his examples can get quite complex, he typically manages to break them down into meaningful pieces. Furthermore, his examples at the beginning of the book tend to go through many rounds of refactoring, which I believe helps the reader to truly understand the programming thought-process and not just the final product.

Whether it was his use of "soaking peas" to explain "Abstraction" or his exploration of Functional and Object Oriented programming, I consistently found myself thinking about programming theory and not about Javascript itself. As I was working my way through this Terrarium example, it didn't occur to me that this book was making me better at Javascript - it occurred to me that this book was making me a better programmer.

Oh, and did I mention that he has a whole chapter on Regular Expressions! Heck yeah!

To end this review, I wanted to present the demo text that Haverbeke used in one of text-parsing examples. Though not important to techniques demonstrated in the chapter, I found the following text to be rather beautiful in its own light (pg 79-80):

% The Book of Programming

%% The Two Aspects

Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisible below.

When the creators built the machine, they put in the processor and the memory. From these arise the two aspects of the program.

The aspect of the processor is the active substance. It is called Control. The aspect of the memory is the passive substance. It is called Data.

Data is made of merely bits, yet it takes complex forms. Control consists only of simple instructions, yet it performs difficult tasks. From the small and trivial, the large and complex arise.

The program source is Data. Control arises from it. The Control proceeds to create new Data. The one is born from the other, the other is useless without the one. This is the harmonious cycle of Data and Control.

Of themselves, Data and Control are without structure. The programmers of old molded their programs out of this raw substance. Over time, the amorphous Data has crystallized into data types, and the chaotic Control was wrung into control structures and functions.

%% Short Sayings

When a student asked Fu-Tzu about the nature of the cycle of Data and Control, Fu-Tzu replied 'Think of a compiler, compiling itself.'

A student asked, 'The programmers of old used only simple machines and no programming language, yet they made beautiful programs. Why do we use complicated machines and programming languages?' Fu-Tzu replied 'The builders of old used only sticks and clay, yet they made beautiful huts.'

A hermit spent 10 years writing a program. 'My program can compute the motion of the stars on a 286-computer running MS DOS,' he proudly announced. 'Nobody owns a 286-computer or uses MS DOS anymore,' Fu-Tzu responded.

Fu-Tzu had written a small program that was full of global state and dubious shortcuts. Reading it, a student asked 'You warned us against these techniques, yet I find them in your program. How can this be?' Fu-Tzu said, 'There is no need to fetch a water hose when the house is not on fire.' {This is not to be read as an encouragement of sloppy programming, but rather as a warning against neurotic adherence to rules of thumb.}

%% Wisdom

A student was complaining about digital numbers. 'When I take the root of two and then square it again, the result is already inaccurate!' Overhearing him, Fu-Tzu laughed. 'Here is a sheet of paper. Write down the precise value of the square root of two for me.'

Fu-Tzu said, 'When you cut against the grain of the wood, much strength is needed. When you program against the grain of a problem, much code is needed.'

Tzu-li and Tzu-ssu were boasting about the size of their latest programs. 'Two-hundred thousand lines,' said Tzu-li, 'not counting comments!' Tzu-ssu responded, 'Psah, mine is almost a *million* lines already.' Fu-Tzu said, 'My best program has five hundred lines.' Hearing this, Tzu-li and Tzu-ssu were enlightened.

A student had been sitting motionless behind his computer for hours, frowning darkly. He was trying to write a beautiful solution to a difficult problem but could not find the right approach. Tu-Tzu hit him on the back of his head and shouted, '*Type something!*' The student started writing an ugly solution. After he had finished, he suddenly understood the beautiful solution.

%% Progression

A beginning programmer writes his programs like an and builds her hill, one piece at a time, without thought for the bigger structure. His programs will be like loose sand. They may stand for a while, but growing too big they fall apart {Referring to the danger of internal inconsistency and duplicated structure in unorganized code.}.

Realizing this problem, the programmer will start to spend a lot of time thinking about structure. His programs will be rigidly structured, like rock sculptures. They are solid, but when they must change, violence must be done to them {Referring to the fact that structure tends to put restrictions on the evolution of a program.}.

The master programmer knows when to apply structure and when to leave things in their simple form. His programs are like clay, solid yet malleable.

This book is a really good guide to Javascript; but even more than that, this book is a great guide to programming. If not familiar with Javascript, some of the examples can get rather complex. But, if you go through them slowly, I believe you'll get a tremendous amount of value out of this book. Definitely a recommended read.

Reader Comments

6 Comments

Thanks, Ben, for the review and great timing!

I decided to buy this eBook today as O'Reilly is donating all non-author proceeds to the Japan relief funds. Great to know that I bought a winner :)!

148 Comments

Wow . . . this author has a way with words! I really liked the tentacle example. To go further . . . one tentacle can only grasp number shapes. So if the tentacle is already holding a number shape and another one is needed, that tentacle gets duplicated.

I think I'll hop over to O'Reilly to add this so I can load it on my Kindle.

6 Comments

Great Review Ben!

I was one of those students who couldn't fully grasp techno-geek like variables back in the day :), so I guess I'll have to get this book.

Thanks!

15,640 Comments

@Craig,

Oh very cool - I didn't realize they were doing that. What a wonderful cause / gesture.

@Lola,

Nice extension. I think you'll enjoy the book.

@Ettiene,

Ha ha, I think this book will help then. Just take it slow - the examples do have a lot of information in them.

15,640 Comments

@James,

I just read the first few books - LOVE it :) I'll have to finish it up later.

@Jen,

Awesome news - thanks for the link. Finally a good excuse to get the "Good Parts" that I've heard so much about over the years.

167 Comments

Ben, whatever you're on, I want me some. Every time I check into your blog you're reviewing a JS book.

It'd be effing sweet if you'd add a little module to your blog system similar to Engadget's on their reviews that lets you give each book a numeric score with pros and cons so we can gauge the relative merit of these books in order to make decisions on which to read from your commendation. It'd also be sweet if you'd add book reviews to your main navigation somehow so we could view them all in a context.

Still the greatest blog ever btw.

15,640 Comments

@Drew,

Oh dude, that's awesome! I didn't realize the book was also available online. Dangy!

@David,

Ha ha ha, thanks :) It's tough, actually, the thought of comparing books. Honestly, my memory isn't good enough to think across multiple books. I read a book, gather its goodness, and then move one. After that, though, the context is lost.

@Phillip,

Awesome - I think you'll really enjoy it.

167 Comments

I actually don't mean that you'd compare them to each other, just a page that lists them all and is search and sortable.

The rating module just lets you say "I give this book an 8/10, pros: uses and objectifies females in all code examples, great coverage of CFML, entertaining, cons: written in pig latin, author's bio pic is creeping me out."

Each review is totally independent of any other.

Here's an example of the ratings module: http://www.engadget.com/2010/08/03/hp-envy-14-review/

167 Comments

Bows @Ben-son. Wax on. Wax off.

Really great quotations that I've learned over this past year+ as I've gained experience as a coder (and reading your blog).

Bows

GONG!
15,640 Comments

@David,

Ha ha ha ha :) Ah, I see what you're saying. That is a cool idea. I wonder if it would feasible (mentally) to go back and retrofit the site. I suppose I could just use something like this going forward.

I'll do some thinking on the matter.

@Randall,

Someone on Twitter was saying that the passage out quoted above was their favorite part of the entire book. There is something really great about it!

383 Comments

Awesome review!!! :-)

I love the part about "warning against neurotic adherence to rules of thumbs" as an against-the-rules kinda girl myself. :-)

The part about lines of code reminded me of my first flash project...800 frames! lol. Could've been done in much less, but I was still learning to use flash! (and it is totally cheesy, too. I love going back and looking at it and laughing. Gives me a good laugh and humor break sometimes)

The part about "'When you cut against the grain of the wood, much strength is needed" took me back in my mind to my days of studying martial arts when we started breaking boards...you are supposed to have special boards and learn to break with the grain, and it is much easier. :-) It still isn't like slicing bread with a knife, but probably still isn't quite as hard as it looks and definitely doable. :-)

Loved this part, also: "The master programmer knows when to apply structure and when to leave things in their simple form. His programs are like clay, solid yet malleable. " Just awesome in general, and a skill I hope I am in the process of picking up.

@Ben, you probably don't have a bad memory at all, it's probably just that your memory is so crammed full of all the stuff you have put into it that you aren't retaining everything any longer. Your memory is probably just flushing stuff at random or stuff IT thinks you don't need instead of what YOU decide to flush or not flush. Unlike in the movie "Eternal Sunshine of the Spotless Mind", where if I remember correctly, I think the whole point was that they were supposed to be able to choose what to flush out of their memories, and supposed to only be flushing out stuff that they wanted to not remember. (I hope I didn't ruin the plot for anybody). You're probably reaching your memory capacity with all the reading, etc. you do. Even if you have a HUGE one, it's still likely you would be reaching it, because you do so much. Definitely not a bad thing...not at all. Also, didn't realize you were a ta. I was a teacher kind of recently at a Community College, so I can see where you're coming from there. :-)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel