JavaScript catches a lot of flak, and for good reason. Early on, developers used it to add inane little features to websites, like snowflakes over the text or a scrolling marquee in the status bar. The code was a mess, and the forgiving nature of the language enabled lots of bad habits. Over time, it grew organically, as if by accident, borrowing patterns from other languages. Programmers figured out how to shoehorn in ideas such as classes (from C++ and other languages) and the model-view-controller pattern (from SmallTalk).

This is different from most other languages, which are architected from the top down by an individual or team. Brendan Eich created JavaScript, sure, but average developers are the ones who turned it into a real programming language. This process is really unorthodox. It’s disorganized. It’s a wonder that it works at all. And you know what? I kind of love it. I love JavaScript for the same reason that I love English, the language that author John McWhorter called “Our Magnificent Bastard Tongue”.

Many English words are borrowed from other languages — even common ones, like “pencil” or “shoe”. We still can’t quite agree on many of the rules — is a preposition really such a bad thing to end a sentence with? There’s no central governing body for the language, as there is with Spanish, so nobody can formally say what’s right or wrong. Those attributes also give English a rich vocabulary and flexible syntax that makes for some clever wordplay and beautiful poetry. They can also make English pretty hard to learn.

Trying to become “fluent” in JavaScript by reading through one of those big lists of frameworks and resources can be overwhelming. It’s like trying to learn English by visiting the Library of Congress. I can’t tell you the exact right place to start, but here’s what has worked so far for me.

Learn the rules, then learn to break the rules.

A high school English teacher encouraged us to learn the rules of grammar before we started taking poetic license with them. Nobody is going to be e.e. cummings right out of the gate. The same is true of JavaScript: it’s important to master basic skills and best practices before doing anything unorthodox. Unlike most languages, JavaScript won’t warn you if you accidentally declare a global variable or miss a semicolon. Tools such as JSHint and JSLint can help develop good habits, as can books such as “Eloquent JavaScript”.

Once you’ve gotten a handle on a language’s vocabulary and grammar, a good next step would be to experiment with different styles of writing, storytelling techniques, or styles of poetry. The JavaScript equivalent of that would be writing different types of programs — say, a number-guessing game, or a jQuery plugin — or learning how to use various patterns. If you’re overwhelmed by the number and variety of JavaScript patterns out there (and I’d be lying if I said I wasn’t), the module pattern and John Resig’s simple object inheritance are great places to start.

After that, by all means feel free to experiment with lots of new frameworks and programming styles. Knowing the foundations of JavaScript will help you learn what’s actually going on under the hood of these frameworks and understand what problems they’re trying to solve. Don’t stress out about learning the details of every framework out there. Just pick one that sounds interesting, figure out how it works, and do something neat with it.

Learn another language.

If you’re primarily a front-end developer, consider learning another programming language. Other languages can be a source of new ideas. Most other languages are also stricter than JavaScript, which can help you build good habits. At a loss for which language to try? How about the one that powers your website’s backend?

A while back, I started learning more about C#, which is used by the back-end developers with whom I work. I was frustrated at at first, because it seemed counter-intuitive in many ways, and the compiler was always complaining that I needed to cast such-and-such a variable to a different type, or that some value couldn’t be null. As I’ve gotten more comfortable with the language, though, I’ve found myself poaching its ideas for use in JavaScript. For example, C# has this concept of “implicitly typed” variables. The idea is that you use the “var” keyword to declare a variable and set an initial value. From then on, the variable retains whatever type you initially set. So for example, saying var myVar = "foo"; is the same thing as saying string myVar = "foo";. Either way, your code will fail if later on you try to set myVar = 42. It’s a nice, concise way to write code in a strictly-typed language.

Anyway, doing that has gotten me into the habit of doing the same thing in JavaScript. Now, when I use JavaScript’s var keyword, I try to set variables to sensible initial values, even if I don’t actually expect to use those values. For example, if I know a variable is going to contain a string, I might set its initial value to "", or an empty string. Doing this helps me avoid “undefined” errors, and (hopefully!) makes my code a little easier to read. Working in a strictly-typed language also makes me think twice before I take advantage of type coercion, JavaScript’s ability to convert one type of variable to another. Do I really need to set that string to 0, or check if that integer is false? This feature can be powerful in the right hands, but it can also introduce some really hairy bugs when used carelessly. (If you’re curious how weird type coercion can get, check out this table.)

Stay Humble.

I used to work with a Ruby developer who told me that the only proper and modern way to write JavaScript was to leave out the semicolons. I replied that if he omitted the semicolons, the JavaScript interpreter would take its best guess as to where they should go and insert them at runtime. He said that he didn’t know that, and then the conversation was over.

At the time, I was proud. I’d made the better argument! I’d won! Now, I regret being such a know-it-all. I wish I’d listened a little more. I had been working with WordPress and writing lots of PHP, and I could’ve learned a lot from someone who worked with a different (and admittedly more modern) stack.

Saying “I don’t know”, or “How does that work?”, or “What do you think?” gives you and the other person a chance to learn and grow. English has a ridiculously huge vocabulary and complicated syntax, and even a fluent speaker is going to need to consult a dictionary or a style guide once in a while. Similarly, JavaScript is full of idiosyncrasies (object equality, for example), and a dizzying array of tools and libraries is available. No single developer is going to understand them all.

Help Build the Future.

More than most other programming languages, JavaScript relies on the programmer to do the right thing — and to decide what “the right thing” is in the first place. As patterns emerge, they’re often used as the basis for libraries and frameworks. Many of these libraries and frameworks have inspired features of ECMAScript 6, the new JavaScript specification that’s currently in development. As with English, you can help shape the future of the language by using it thoughtfully and creatively. You don’t have to be a Shakespeare to shape modern English, and you don’t have to be Douglas Crockford to shape JavaScript.

JavaScript is a cosmopolitan language, influenced by the diverse people who speak it and the far-flung places where it runs. It’s democratic. It’s versatile. And sometimes it’s just truly, unbelievably weird. I can think of no better language for the web.

License: All rights reserved