A JavaScript survival guide

[2014-07-14] dev, javascript
(Ad, please don’t block)

Are you a programmer who is considering learning JavaScript, but unsure whether it is worth the pain? Then this blog post is for you: I argue that it is worth it and give tips for surviving the language.

Why learn JavaScript?  

The present  

The main reason for choosing JavaScript is the breadth of its ecosystem: the web platform, Node.js, JSON, NoSQL databases, Cordova/PhoneGap, automating PhotoShop and many other apps, etc.

Compared to Java, I like the interactivity of JavaScript and the web platform. It is easy to quickly try out things and to inspect what is currently going on.

I also like the flexibility of the language. It means that where you have deep inheritance hierarchies and other relatively complex patterns in more static languages, you can often get by with more lightweight solutions in JavaScript.

The future  

ECMAScript 6 [1], the upcoming version of JavaScript fixes many of JavaScript’s problems: not enough data structures, limited built-in support for inheritance, no built-in support for modules, shadowing this, functions playing too many roles, …

(As an aside: many of JavaScript’s deficiencies can be fixed via libraries and tools, but built-in classes and modules will make JavaScript code more portable and – hopefully – reduce the fragmentation of the community.)

Additionally, there is much innovation and experimentation around:

  • Language: near-native performance via asm.js [2], easy parallelization via ParallelJS [3], macros via Sweet.js, …
  • Web platform: Web Components [2:1], …

Take your time to get to know JavaScript  

An important insight for learning JavaScript (paraphrasing Golo Roden):

JavaScript may look familiar, but it really isn’t. Take your time to get to know it.

The learning curve for JavaScript is due to:

  1. Power: it’s a very flexible and powerful language.
  2. Quirkiness: it’s also a spartan and quirky language.

People who hate JavaScript tend to think that JavaScript is all #2 and no #1, but if you learn it properly, you’ll appreciate #1 and get relatively simple rules and techniques for working around #2.

JavaScript’s power  

JavaScript is an interesting mix of object-oriented programming (OOP) and functional programming (FP). JavaScript creator Brendan Eich’s favorite parts of JavaScript are:

  • First-class functions (FP)
  • Closures (FP)
  • Prototypes (prototypal OOP)
  • Object literals (prototypal OOP)
  • Array literals (FP)

JavaScript is one of the few languages that allow you to directly create objects, which is nice for prototyping and incremental development.

Testament to the power of aforementioned parts is how many missing features were added using them:

  • A better for loop (the array method forEach())
  • Inheritance libraries (classes, mixins, etc.)
  • Module systems

JSON, a JavaScript-native data storage format could also be mentioned in this list.

How to survive JavaScript?  

If JavaScript has become inevitable for you and you still find its shortcomings hard to accept, what can you do to minimize the pain? This section gives a few tips.

Learn as much as you can  

These are a few things that helped me with learning the language:

  • JSbooks” is a list of free online books on JavaScript.
  • Stick to the good parts: pick a safe subset of JavaScript and use it exclusively. That doesn’t mean you can afford to completely ignore the rest of the language, but you don’t have to burden your brain with all of its details.
  • Read JavaScript style guides: You probably won’t agree with everything they say, but they will give you ideas what to look out for. Three good style guides (among many):
  • Get in contact with the community: the diversity of the web means that the JavaScript community is also diverse, which makes them inspiring and instructive to hang out with. There are many opportunities to do so:
    • Twitter: is a great way to stay up to date on web development topics and to find out what the community is talking about.
    • User groups: many of the larger cities have JavaScript user groups. CommunityJS.org is directory of them.
    • Conferences: JavaScript has arrived in the enterprise, which means there are now more JavaScript conferences than ever, for all tastes and wallet sizes. Lanyrd has a list.

More safety  

There are several ways in which you can make the language safer:

  • Strict mode: you’ll get more errors and a slightly cleaner language.
  • Linters (style checkers): analyze JavaScript source code and detect problems and anti-patterns. As a consequence, they offset some of the disadvantages of the language’s dynamic nature and teach good style. Three well-know linters are:
    • JSLint: the classic, the first JavaScript linter, by Douglas Crockford
    • JSHint: currently the most popular linter
    • ESLint: key strength – easily extensible
  • Testing: is an important weapon for making dynamic languages safer to work with. You’ll usually write more tests than in a static language. On the other hand, it’s relatively pleasant to write tests in JavaScript.
  • Libraries and frameworks: shield you from some of the quirks of the language and browsers.
    • Examples: Underscore.js, jQuery, AngularJS, Ember.js, …

More comfort  

JavaScript is a relatively spartan language. Thus, it’s good that IDEs (such as Microsoft’s Visual Studio and JetBrain’s WebStorm) are constantly improving and make dealing with it simpler.

Additionally, even though ECMAScript 6, the next version of JavaScript, hasn’t even been formally standardized, yet, you can already use it, by compiling it to ECMAScript 5 [4]. That means that you get to work with a much more full-featured language.

Further reading: “Speaking JavaScript”  

Especially initially, I wrote the book “Speaking JavaScript” (free online) for myself, to document my exploration of the JavaScript ecosystem. I also wanted to create the book that I wish I had had when I started learning the language: providing comprehensive understanding, while assuming that you already know how to program. The following chapters are useful for getting started:

References  


Acknowledgement. This blog post is based on a discussion I had with my colleague Béla Varga.


  1. ECMAScript 6: what’s next for JavaScript? ↩︎

  2. asm.js: closing the gap between JavaScript and native ↩︎ ↩︎

  3. ParallelJS: data parallelism for JavaScript ↩︎

  4. ECMAScript 6 tools” by Addy Osmani. ↩︎