Looking Into the Strengths of JavaScript
Aug 26 2011 11:12pm | | Share

Looking Into the Strengths of JavaScript

By Sean Hopen

I've been digging deeper into JavaScript a bit lately, because it's becoming a big player out there. HTML5's new capabilities have the rich internet development world looking at what can be done with it, and JavaScript is what you need to really make it work. People are even using JavaScript for server-side development with node.js.

I never took JavaScript very seriously. The syntax seemed a little flakey, it wasn't really object oriented, it wasn't strongly typed, and it was hard to debug.  I missed out on some of the strengths of the language. I may not have, had I known that JavaScript was influenced heavily by Lisp, a language I loved when I was doing AI programming.

I'd say the key factor in my appreciation of JavaScript is the functional programming style, including the use of closures. These are all new things, or partly new, for folks that are steeped in the C tradition (including visual basic, C++, java, etc.). There are also new tools to work around the syntactic problems with JavaScript (the language was designed in 10 days!). Check out tools like JSLint.

Note: You can find out more about this, and other good stuff about JavaScript from Doug Crockford's highly recommended talk. I also recommend Alex Russell's talk too.

So why do we like Functional Programming and closures? These are topics for another blog post each, but in short:

Functional programming is cool for at least two reasons:

1) There is a resurgence of interest in FP because it's easier to adapt to concurrent programming on multiple processors and for asynchronous communication.

2) Its a style of coding that can be incredibly expressive with very few keystrokes.

Closures, also two reasons:

1) In JavaScript it's essential to scoping variables. Having nothing but globals is hideous to software engineers and death to apps of any size at all.

2) Same point as above. Its part of the powerful expression you can get with functional programming. You just have to get used to passing around functions instead of objects. Closures allow you to encapsulate data in those functions.

I've started playing around with a really good example of the power of functional programming; something called Reactive Extensions (Rx).  I've been obsessed for years with finding a better way to write code that handles asynchronous operations (as we do every day on the web) in a way that the code captures the intention of the design (which we don't see every day). Functional programming completely changes the way I think about asynchronous operations.  I'm going to play around with this a bit before writing a blog about this. If you're already convinced, Google Reactive Extensions for JavaScript. There's also good introduction here, and a video by the guy that invented it here.

I would love to hear your thoughts on what I've written.