JavaScript Review

We'll be using JavaScript throughout this class. Since JavaScript is our tool, we should make sure we're ready to use it. The more you can remove the friction between your thoughts and your code, the better and faster you can learn new concepts and code them up.

Not a JavaScript expert? Don't panic. We're not going to be straining to find the most obscure dark corners of the language so we can write code that nobody understands. The goal is to get things working as quickly as possible with code that is as simple as possible. All the references below are to help you get up to speed and make it easier to get your project working. You don't have to do everything or read everything. There is no quiz :)

On-line Guides

Eloquent JavaScript

Eloquent JavaScript is a great book available free online. It includes examples and programming problems with solutions. It covers all the basics of JavaScript and then some. The free on-line version is excellent, and there is a paid version that is available in print and as an ebook. The paid version updates and expands the text, and is also highly recommended.

Mozilla JavaScript Guide

The Mozilla JavaScript Guide is a handy introduction to JavaScript concepts. I wouldn't read it straight through, but it's a great refresher on specific concepts. It has nice code examples.

Crockford on JavaScript Videos

Douglas Crockford has a nice series of videos on the history of JavaScript, its features, and its use. Lots of great content in the talks.

Recommended Books

JavaScript: The Good Parts

My favorite JavaScript book is JavaScript: The Good Parts by Douglas Crockford. It's short and sweet, packed full of good content.

JavaScript: The Definitive Guide

A good reference is JavaScript: The Definitive Guide by David Flanagan. This is not a book to sit down and learn JavaScript, but it is thorough in its coverage.

Interactive Tutorials

Nathan's Lessons (What's a closure?)

If you haven't already, you should take a look at Nathan's Lessons, my interactive lessons on closures in JavaScript. If you can finish all the lessons you are doing very well. I think closures are the most powerful and elegant of all of JavaScripts features.

Secrets of the JavaScript Ninja tutorials

This is a fun series of interactive tutorials based on John Resig's upcoming book Secrets of the JavaScript Ninja. The tutorials cover lots of neat techniques in JavaScript.

Programming Environment

A great way of reducing the distance between your ideas and working code is to spend some effort setting up a nice environment for programming JavaScript. Getting an efficient environment that is comfortable for you to use is essential for rapid development.

Here are some tools I've found helpful.


Firebug is a great tool that lets you interactively write and debug JavaScript code. All web browsers have JavaScript consoles and debuggers available, but my favorite is Firebug. Whichever browser you like using, find out how to get to the JavaScript console and get familiar with the tools that are available.


We'll be using Node to run JavaScript programs from the command line. What can I say, I'm old school. Of course Node also offers all kinds of event-driven I/O capabilities, but we'll ignore all that and use it to run tests.

The other great thing about Node is that it has an ecosystem of tools and a convenient package manager npm (the Node Package Manager) so they are easy to install. We'll be using npm to install several different packages for our project. In particular we'll be installing and using PEG.js for creating parsers, Mocha for JavaScript unit testing, and Chai for JavaScript assertions.


JSHint checks your JavaScript code for simple bugs. You can paste in your code to the online form, it's easy. For automated use at the command line you can install a local copy using npm. It has lots of options for turning different checks on or off based on your personal preference.

Challenge Problem

In order to give you a sense of what kind of code we'll be working with in this class, I've put together a challenge problem on binary search trees. If the following explanation and problem make sense and are easy for you, you'll set! If not, don't despair. We'll be using JavaScript in a way that it's not often used, so don't feel bad if it seems strange. This example is actually very similar to some of the later code we'll write.

Binary search trees pop up everywhere in computer science. A binary search tree is made up of nodes. Each node has some data and up to two child nodes, named "left" and "right". Here's a graphical illustration of a happy little binary search tree holding a few letters of data:

An important property of a binary search tree is that for every node, everything in the left subtree is less than the data at the node and everything in the right subtree is greater than the data at the node.

A simple way to encode binary search trees in JavaScript is to create objects that have data, left, and right fields. We'll set left and right to null if there is no subtree on that side.

Here's a little tree with two nodes.

Now we can build up functions that work on binary search trees. Here's the function contains that goes down the tree to see if a given value exists in the tree.

Of course there's more than one way to skin a cat. Here's the same function implemented in a recursive way.

Now you try.

Write a function count that counts the number of elements in a binary search tree. If you're ambitious, solve it once with recursion and once without recursion.