TulipeMoutarde.be

About

Javascript, The Good Parts

Written on January 11 2011.

This blog post was written a long time ago and may not reflect my current opinion or might be technically out of date. Read with a grain of salt.

I've just finished to read Javascript: The Good Parts of Douglas Crockford. If you’re doing Javascript development you should read it if you haven’t.

This book is not a definitive guide to Javascript but will show you which part of the language to avoid if you want to eliminate a whole class of problems.

Here are some striking (at least for me) examples:

'' == '0'   // false
0 == ''     // true
0 == '0'    // true

That’s because == tries to coerce the values if they are of different types.

return
{
  status: true
};

Will not return an object with a `status` field. It will return `undefined`. Do you know why?

Another nice one is the `parseInt()` function.

parseInt('07') // returns 7
parseInt('08') // returns 0
parseInt('09') // returns 0

That’s because when the first character of the parsed string is a zero, it is evaluated in base 8. Seriously. In javascript. A high level language. Wow.

Don’t worry, the book is not only a compilation of the weird design choices of Javascript. It will guide you through a world without global variables and will show you how to use functions to create modules. You will learn to master the prototypal object model and understand why the `new` construct in Javascript is evil.

Highly recommended.