Search form

You are here

Javascript Why To

Peter Mallett

Senior Drupal Developer

Jun

05

2014

Javascript Why To

You can easily find information with varying degrees of correctness about “how-to” with JavaScript. I want to make a case for why-to learn to write in JavaScript. Also note that if you’re in a place like I was just a few years ago with JS, you might not actually know JS yet.

There are seemingly an ever-increasing number of efforts with the purpose of allowing programmers to write in some other language, like Java, CoffeeScript, C, etc., and then have a program turn that code into JavaScript for us.

I think in a lot of cases* this practice will fade once more programmers learn the JavaScript language itself. You just stop wanting it to be something it’s not.

* ASM.js, in contrast, seems to be a case where something special is happening that can not easily be replaced by simply writing correct JS manually.

You know nothing, purported JavaScript programmer.

In 2010 I stumbled onto the idea that I didn’t actually know JavaScript. At first this idea was really alien to me because: how could I not? JS had been around forever, it seemed, and I had certainly used it for… stuff. However, it turned out to be true. I didn’t know how to write in JavaScript. It’s possible to use JS nearly every day, as I was then, and yet still not really know the language.

This idea was conveyed to me by Douglas Crockford when I stumbled onto his Crockford on JavaScript talks (then posted on the YUI Theater site, now more conveniently available on YouTube). If you have never seen these talks, and you’re interested in learning JS (and a little about programming history along the way) I highly recommend checking them out.

It’s not me, it’s you.

“I hate JavaScript.” - everyone, at some point

I’m sure we’ve all heard this. I’ve even said it, though as I alluded earlier, I knew nothing. Also, in my own defence: AJAX and The Good Parts had not yet been discovered (yes, I’m old).

One of the phenomenons that Crockford points out is that a lot of people (most?) think they should be able to do productive work with this language without actually learning it first. It is both a testament to the power and elegance of JS and also a very strange line of thought for programmers to have.

I have found that the more I learned about the language, how it works, and, sometimes more importantly, why it works the way it does, the more my “hate” melted away and was replaced with appreciation. The wrestling match I once had with the syntax in order to make it appear more like a language with classes went away. My instinct to define functions with a name somewhere in the code and then call them from somewhere else, even when they were never going to be called again from anywhere else ever, went too.

I’d be willing to bet there are even more habits and conventions from other languages I've tried to blindly apply to JS that have since faded away.

Toy language.

“Generally the most important new innovations are received with contempt and horror.”

- Douglas Crockford

It’s sort of strange to me now that I also once subscribed to this idea. The idea that somehow some languages were inherently inferior to other “real” languages. In all ways. Especially given that almost all of the professional programming in my life has been done using PHP.

I think the fact that languages like PHP, Python, JavaScript, etc. are referred to as “scripting languages” is sort of vestigial at this point. Almost every programmer is working on things where it is actually more beneficial for code to run on a virtual machine. This instead of directly controlling the actual machine, where lots of very bad things can go very wrong (hello, Heartbleed…). So why would a language for a system that only operates in that way necessarily be a bad thing?

The Good Parts.

I think for a lot of folks, the issue with JS comes down to the fact that it has been laden with some very bad parts. Let’s be honest though, all languages are, and JS is getting better (see ES5 strict mode). And that brings me to my other Crockford-related recommendation towards the path of figuring out JS. His book: “JavaScript: The Good Parts”.

The Good Parts is a fairly concise description of the JavaScript language, taking care to point out the Good Parts and what makes them good. The book then takes a detour into the Bad Parts, describes what makes them bad, and gives some compelling arguments on why you should avoid them.

This book, along with the talks mentioned above, set me on a path of thinking about JS in a way that I never had done before. Not as a bad, or “fake” language, but as a real language, and also an important language. But, also a different language than what I was used to, a point I had not stopped to consider.

Other materials

From The Good Stuff, I followed up with reading some other things and about some other subjects:

After going through all of this, and thinking about functional programming in general, I can say I’m glad I have. My programming with JS had improved quite a bit and, perhaps more importantly, my experience with using JS has improved: I’m happier with it now.

Why JavaScript? Well, like it or not, it’s everywhere. It’s said to be the most used programming language in the world now.

I guess what I’m trying to say is, it’s alright, there’s plenty to like. It’s ok to like it!