Thoughts on development, design and the world we live in.

JavaScript Koans

By glenna in JavaScript, Web Development. Posted on December 28th

I’m an iOS engineer with a background in robotics who is just diving into front-end web development. At Blazing Cloud, we’ve found that the approach taken by Koans, learning through testing, to be an effective way to understand the core syntax of a new language. Neither of the JavaScript Koans are nearly as fleshed out as the Ruby Koans (http://rubykoans.com/), but they are still good practice before diving into real-world Javascript challenges.


The set of JavaScript Koans created by David Laing (@davidlaing) & Greg Malcolm (@GregMalcolm) is definitely my favorite, starting with the well documented Readme file and ending with the “Applying what you’ve learned” test. The test framework used is the Jasmine framework (http://pivotal.github.com/jasmine/), and is seemingly very similar to the Objective-C testing framework, Kiwi (https://github.com/allending/Kiwi), that I have used in a few projects.

Here are some highlights:

  • The test on arrays is expansive covering: shift, unshift, slice, passing by reference to functions, indexing, copying, push, pop and length.
  • Includes tests for higher order functions on arrays using the Underscore library (http://underscorejs.org/), which covers: map, reduce, filter, chain, flatten, range, any and all. These are all very useful functions for people who are constantly manipulating data, or maybe it’s because I’m coming off of a brief Ruby stint where the code base used these handy little tricks all the time, and I’m fairly new to the concept.
  • Tests on mutability and public/private accessibility of properties in constructors
  • A more comprehensive set of tests on objects including: functions as properties (i.e. methods), using prototype, the “in” operator and explaining the “this” role in methods.
  • Finally, the “Applying what you’ve learned” test. Really, what is the point of doing the Koans unless you can prove you learned something? I was excited to dive into those new-to-me functions like map, flatten, and reduce. They give examples of how you would solve the problems using the oh-so-familiar for-loop and if-statements, and you are charged with solving the same problem using the newly learned tools and tricks.


The JavaScript Koans created by Liam McLennan (@liammclennan) uses the QUnit testing framework (http://qunitjs.com/). I definitely preferred looking at the test output of these Koans. QUnit collapses green tests to let you focus on the first failing test, but you can always expand the green tests to see what was tested.

I would say that this set of Koans is geared toward the beginner level programmer. Here’s why:

  • It includs a brief set of tests on operators like: +, +=, -, -=, %=.
  • Includes tests on control structures. Most somewhat experienced programmers can pick up the syntax quickly, without dedicated tests.
  • Explains the very basics of arrays: indexing, push, pop and length. It did, however, include splice as well.
  • Has a brief test on numbers: what is the class of numbers, NaN
  • The object test is basic: typeof, how to add properties, and functions as properties

I’d say the strongest point of Liam McLennan’s Koans was that they explicitly tested using the ‘call’ and ‘apply’ methods to invoke functions.


Here are some overall complaints about the comprehensiveness of the available JavaScript Koans:

  • Even though I worked through both of the Koans, I still don’t properly understand what prototype does. If it’s important enough to test, shouldn’t the tests help describe the concept better?
  • They both do a decent job of describing basic inheritance in JavaScript, but we all know that inheritance gets a lot more difficult than “does the child have the parent’s property”?

Overall, I’d recommend the Koans created by David Laing & Greg Malcolm for more experienced programmers, and those created by Liam McLennan to those just starting out in the programming world. However, it takes less than a day to do them both, so it wouldn’t hurt to go through both of them.

Now it’s time for me to get to work on those extra credit Koans!

One Comment

  1. Posted December 28, 2012 at 3:40 pm | Permalink

    I’m glad that you got something out of the koans. If you, or any of your readers, can think of anything that is missing from the koans I’m always happy to receive pull requests. Thanks for the review.

Post a Comment

Your email is never shared. Required fields are marked *