Javascript, The Good Parts

I attended a talk by Douglas Crockford today, entitled "JavaScript - The Good Parts", where he outlined the good and bad parts of JavaScript. His slides are located on his site, at

This article will highlight some of the key points made during the presentation and as there was a lot of extra material, not reflected in the notes, I will try to capture that as well.


These are the languages that influenced the development of JavaScript and in parenthesis are the elements that were inherited.

  • Java (syntact, conventions)
  • Self (prototypal inheritance, dynamic objects)
  • Scheme (lambda, loose typing)


Lambda is perhaps one of the most important features as it allows for the functional programming that we all know and love. This includes declaring functions inside of functions, closures, and returning/passing functions as parameters.

The Bad Parts

  • Global Variables - always bad practice in all languages
  • + adds and concatenates
  • Semicolon insertion - produces poor and unexpected behavior
  • typeof - doesnt work as expected (array and null are objects)
  • with and eval - frequently misused
  • phony arrays
  • == and != - strings and numbers shouldnt be equal
  • falsy: false, null, undefined, NaN, 0

Bad Heritage

The methods and syntax inherited from older languages that frequently cause issues or are just plain poor.

  • blockless statements - poor practice and often causes unexpected issues
  • expression statements
  • floating point arithmetic based on IEEE standards - (0.1 + 0.2 !== 0.3)
  • ++ and -- - leads to complex code blocks
  • switch pass-throughs - causes frequent unexpected behavior

Good Parts

  • Lambdas
  • Dynamic Objects
  • Loose Typing
  • Prototypal Inheritance

Crockford also suggests to avoid using the new operator as it can lead to issues in the global namespace. Here is his method

Object.prototype.begetObject = function() { Function F() {}; F.prototype = this; return new F(); }

In this case begetObject returns the new instance of the Object, basically inheriting from itself. A better implementation could be to pass another Object into the constructor, then you can have objects inheriting from each other. I will discuss this more in a feature article.

Use that instead of self

He also pointed out that my technique of using self variable name to reference the closure safe version of the this Object is a bad practice because some browsers reference the window as self, which could cause errors. Instead he recommends using a variable named that.

Garbage Collection - The IE6 Memory Leak

Different browsers use different garbage collection techniques, which sometimes cause issue (IE6 memory leak). IE6 uses a technique known as, Reference Counting. This is a very simple technique that counts references to objects and deletes objects with no references. In JavaScript, this does not work well, because of event listeners. If an element is removed from the DOM that still has an event listener, then the element is not removed from memory. And this causes the IE6 memory leak.

Use K & R style braces

I always thought that the bracket structure we used in JavaScript was a stylistic issue and not a functional one. However, do to semi-colon insertion, there can be some serious issues. Therefore, K & R braces should be used over Allman or another style. Here is a famous example:

K & R style:

var foo = function () {
	return {
		ok: true

is not the same as

Allman Style:

var foo = function ()
		ok: true

in the Allman example what actually happens is that the browser inserts a semi-colon at the end of the return line and the inner object simple becomes an unreachable and useless statement. JavaScript won&rsquot;t through any errors as it is syntactically correct, but it will not do what you need it to. Therefore, you should use K & R braces so as to be safe.

Well, that about wraps up what I learned today. Hopefully, this will help you as well. I will also be writing an article about Prototypal Inheritance in November, when I have some time to put together some tests.