Elisabeth Robson

Is JavaScript a good first programming language?

Posted in Blog by Elisabeth on March 28, 2014

New programmers have a large number of languages to choose from, some easier to start with than others. Every language has its pros and cons as a first programming language, and if you search the web for “good first programming language” you’ll find as many answers as there are programming languages. Everyone’s got an opinion. I thought I’d add my 2 cents to the mix, and describe why I think JavaScript is a good first programming language.

First, JavaScript is easy to get started with: all you need is a text editor and a browser. You don’t need a compiler, or a special environment to run JavaScript.

JavaScript is freely available. You don’t have to pay anything to use JavaScript. You can download a browser for free, and every computer operating system comes with a text editor built-in.

JavaScript is a useful language. Despite its (now out-dated) reputation as not being very powerful, you can indeed do useful things with JavaScript. Everyone who accesses web pages is using JavaScript, and more often than not, JavaScript is adding useful functionality those pages. For instance, if you’re using interactive maps, you’re using JavaScript (Google maps, Bing maps, OpenStreet maps all use JavaScript). If you’re filling in and submitting a form, you’re using JavaScript (to make sure typed in your phone number correctly, or even to send your information to the server). If you’re using an image manipulation application in the browser, you’re using JavaScript. And JavaScript isn’t just useful in browsers; it’s also useful on the server side. With JavaScript and Node.js, you can read and write data from and to files, and communicate with a web application in a way that scales efficiently. You can also use JavaScript to create mobile apps. Using a service like Phonegap, you can write an application once with HTML, CSS, and JavaScript, and deploy the app on Android, iPhone & iPad, and even Windows 8.

From this list of things you can do with JavaScript, you can see that it’s not just useful but also versatile.

JavaScript is fast. It used to be slow, but now it’s fast. Really fast. So you can’t use that excuse not to learn it anymore.

It’s also widely used. If you want to do anything in a browser, you need to know JavaScript. Even if your product has nothing to do with the web (maybe you’re building a spreadsheet program), you’ll want to have a web page for your product, right?

A good question to ask when picking a language to learn is: will it continue to be used in the future? After all, you don’t want to spend a lot of time and energy learning a language that you won’t be able to use for a long time. There are good reasons to learn esoteric languages, but for a first language in particular, you’ll want to get a lot of use out of the language for all that effort you invest in learning it. It’s highly likely that JavaScript’s going to be around for a long time. The development effort that is going into JavaScript on both the client and server, and into JavaScript frameworks, like Angular.js, Backbone.js, jQuery, and many more, is a good indication of the support this language has.

Another criteria you might consider when choosing a language to learn is whether you can transfer what you learn about JavaScript to other languages. While JavaScript has many unique features, the core concepts – object-oriented programming, functional programming, C-based syntax – all are transferable to a wide range of popular languages.

And finally, JavaScript has a strong community. Many people are using JavaScript, and most of those people are also talking about JavaScript… in forums, tutorials, blogs, and articles. Some are also building libraries and frameworks, writing about JavaScript, teaching JavaScript, and are available to answer your questions about JavaScript. There is no doubt that if you choose to learn JavaScript you’ll be joining a large community of people who can help you learn, and point you to a wide range of resources to choose from as you learn.

So, there are many reasons why JavaScript is a good first programming language. But there are also a few slightly weird things about the language that can confuse beginners. Here are handful:

Dynamic types

Because JavaScript is a dynamically typed language, you can do things in the language you can’t do in a statically typed language like Java. For instance, you can declare and initialize a variable to a number:

var x = 3;

and then turn around and assign a different kind of value, like a string, to that variable in the next statement:

x = "Hello!";

and JavaScript won’t complain. So what? Well, this can cause problems if you’re assuming the variable x contains a number or a string at a particular point in your code. You can’t guarantee that x will always be a number or always be a string.

Having dynamic typing isn’t a bad thing, but it means that you can make errors that can trip you up and confuse you, especially if you’re a beginner. You don’t have a compiler making sure you’re using only one type of value for a variable, so if you mix up the types of values that you assign to a variable, this can cause problems in your code if you’re not watching out. A good way around this is to try to make sure you stick with one type of value for a given variable, and, when it really matters, check to make sure a variable has the kind of value you expect in it before you operate with it.

Equality and type conversion

Speaking of checking variables, another way beginners can get easily confused is with equality and type conversion.

If you have a variable, testMe, that’s initialized to the number value 99:

var testMe = 99;

and you write:

if (testMe == "99") {
  alert("A number is equal to a string!");
}

what do you think will happen? Will you see the alert?

Some might say no. After all, how could a number possibly be equal to a string? But if you try this code, you’ll find that you DO see the alert. Which means that the number 99 is equal to the string “99”, at least according to JavaScript.

This is probably very confusing for beginners. And not only that, when you dig into WHY this happens, you’ll discover you have to memorize a few rules so you can understand how to tell when one thing equals another thing. You’ll discover that while 99 is equal to “99”, 99 is not equal to the string “vanilla”. (That’s good!) But you’ll also discover that 1 equals true, and undefined equals null, and…well, at this point, your head might start to hurt a little.

One way around this, aside from learning and understanding the rules for how the equality operator (==) converts its operands, is to use strict equality (===):

if (testMe === "99") {
  alert("A number is equal to a string!");
}

When you use strict equality, you won’t see the alert, because now, both the type and the value of the two values being compared has to be the same for them to be considered equal, and in this case, the type of the number is not the same as the type of the string.

Having two different kinds of equality operators is a bit strange, and adds to the complexity of what beginners need to learn.

Falsey values

Another example of where JavaScript can trip up beginners is falsey values. You know that if you write:

var dog = "Fido";
if (dog === "Fido") {
  alert(dog + " says woof woof!");
}

you’ll see the alert, “Fido says woof woof” because the string stored in dog is “Fido” so the equality is true.

But, what happens if you write:

if (dog) {
  alert(dog + " says woof woof!");
}

Will you see the alert? And what does this code, if (dog)… even mean? The value of dog is a string, not true or false, so how can you use a string “Fido” in a conditional?

Again, this can confuse beginners! It turns out that JavaScript values are “truthy” and “falsey”. Most values are truthy, including strings like “Fido”. There are five falsey values, and it’s worth memorizing them all so if you see (or write) code like the above, you’ll know what to expect.

There are a few other areas where JavaScript can be a bit confusing at times, but on balance, I think it’s worth getting past these gotchas to get all the benefits that come with learning the language. For one thing, if you know HTML & CSS, you’ll immediately be able to start using your JavaScript skills to create interactive pages and communicate with server-side code, which is pretty fun.

If you’re a new programmer and you know a little bit of HTML and CSS, you’re in a great position to learn JavaScript using Head First JavaScript Programming, which assumes no prior programming experience. Let me know what you think!

2 Responses

Subscribe to comments with RSS.

  1. Richard Fuhr said, on June 27, 2014 at 5:25 pm

    The “dynamic types” feature of JavaScript has made it easier for me to commit a number of serious errors when developing my initial JavaScript app (see website link). Also, the non-standard way (compared to C++, Java, etc.) of declaring classes takes some getting used to. That leads to my question to Elizabeth and readers of her blog. Do you think that TypeScript will catch on as a layer above JavaScript? It seems well thought out, and it enables developers to write code where errors can be caught at compile time (or perhaps we should say transpire time) since TypeScript source gets converted to JavaScript via the tsc command.

  2. Elisabeth said, on June 27, 2014 at 8:10 pm

    Hi Richard,
    That’s a good question! I’m not sure I know the answer. Some people love the flexibility of dynamic typing; others, used to static typing, hate it. And then there’s people floating in between (like me). I’m so used to the dynamic typing in JavaScript now, I don’t really think about it, and yet when I am working in Java, I transition easily to the world to static types.

    So I guess the answer may depend on your background. If you’re learning JavaScript, and you already have a background in dynamically typed languages (e.g. Ruby), then you may transition more easily than if your background is in Java or C#.

    That said, I do think that as JavaScript projects grow in size, there may be some benefit to a layer of type safety on top, especially if you’re working with a team. So I think it might be useful in some contexts. I’m not sure TypeScript and the like will catch on ubiquitously but there maybe a subset of people who like the additional type safety it brings.

    Also, there are several great unit testing frameworks for JavaScript now that may help you deal with errors, because you can write tests that specify details of what you expect under certain conditions. Check out Mocha, for instance (among others).

    Elisabeth


Leave a Reply

Your email address will not be published. Required fields are marked *


2 + four =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>