Let’s be honest.

JavaScript is a weird programming language. 

Douglas Crockford, the creator of JSON and one of the most well-known JavaScript programmers, famously said in his book JavaScript the Good Parts:

“JavaScript is built on some very good ideas and a few very bad ones.”

This much is true. At its core, Javascript is very different from pretty much every other programming language out there.

So why did the programming community adopt it?

The short answer is…well, we didn’t have a choice. 

For a long time, if you were a developer who wanted to write code that would execute inside a web browser, you needed to use JavaScript. Your hands were tied.

However, after being coerced to use this funky programming language against my will for years, I can say that at JavaScript is actually a pretty neat programming language…despite the fact that it’s so different.

I’ll explain why, but let me start by explaining what makes Javascript so different.

javascript programming language

What is JavaScript?

At its core, JavaScript is an asynchronous, single threaded programming language that leverages an event queue to do a lot of the work. 

That might sound like a lot of meaningless buzzwords, but they’re not. You need to understand what all of them mean in order to really understand how Javascript works. 

Recently, a Firehose student asked me about how JavaScript works. I’ve removed the student’s name and image for privacy reasons.

javascript programming language

JavaScript is a strange contradiction. The contradiction is one of the things that makes JavaScript powerful and enjoyable. The student’s question cuts to the core of the strangeness of the language.

Let me get into that a little deeper.

JavaScript is a Single Threaded Programming Language.

In a sense, this means that it can only do one thing at a time.

JavaScript is also an Asynchronous Programming Language.

In a sense, this means that JavaScript can do more than one thing at a time.

How can this be?  None of this makes sense!?  

Not so fast, there’s a method to the madness.

So what does this actually mean?  Well, it’s easier to describe with an analogy. 

For argument’s sake, let’s say a human cannot do more than one thing at a time.

Think about how you cook dinner. Let’s say you want to cook something in the oven that will take 30 minutes to finish. And let’s say you also intend to read a book.

If you were to follow a traditional programming, you would have you prepare your meal, put it in the oven, wait for it to finish, eat your meal, and then read a book.

JavaScript programming is different.

 In this case, you could prepare your meal, put it in the oven, set a timer for 30 minutes, and read for 30 minutes. When the oven alarm went off, you would stop reading the book, return to the oven, eat your meal, and then start reading again.

Since you’re not accomplishing anything by standing in front of the oven and waiting for your meal to finish, why not do something else instead? That’s the JavaScript approach to these types of situations.

But instead of things taking upwards of 30 minutes, the “waiting” type tasks (the equivalent of cooking) often take a second or less in JavaScript.

Being only able to do one thing at a time covers the base of JavaScript being a single threaded programming language. 

Not waiting for something that will take a long time to finish, and moving on to the next thing (and getting reminded by something like an alarm to pick up where you left off when the event finished), is the approach to being asynchronous.

This supposed “contradiction” works because of an underappreciated nuance. When I explained this to the student, he brought up an interesting subtlety that explains how these ideas can work in a non-contradiction.

Read it and this will start to make more sense.

This is absolutely spot on.

JavaScript programs only consciously use direction or control. Once something no longer needs direction or control, it can move onto the next task. It’s also one of the things that allow technologies like JavaScript to be super efficient.

What are some common programming situations in which you need Javascript to take this approach?

Say you’re storing information in a database and you wanted to load all the data from the database. Since the database is an external entity, the database itself will need to perform some work to get the items.

In order to do this with most programming languages, like Ruby, you would need to do three things:

  • Reach out to the database
  • Wait for the database to come back with the data
  • Move onto the next step

But if you’re using something like JavaScript and NodeJS, the procedure would be a little different. Instead, you would:

  • Reach out to the database
  • Move onto the next step
  • When the database comes back with the data, it would ping your code and you could do whatever you need to with the data.

The same is true if you’re making API requests to a different server. It may take a while for the request to finish, so it’s best not to wait. Instead, JavaScript will initiate the request, go on to other stuff, and pick up where it left off once the request finished.

JavaScript does this type of thing… ALL THE TIME.

At first, it can feel a little uncomfortable. It runs counter to most programming principles that you’re trained in as a developer.

Most programming languages are languages of do this, then do that.”

But Javascript is a programming language of “when.” You specify what should happen when specific things happen, instead of specifying one step after another. It’s a paradigm shift in a programming mentality.

What makes Javascript hard to adapt to?

At its core, programming is about breaking large abstract problems into concrete articulated steps and instructions. The classic example used in classrooms is thinking about making a peanut butter and jelly sandwich. When I was asked this as a child, I responded with something like:

To make a peanut butter and jelly sandwich, you spread peanut butter and jelly over two pieces of bread and then combine them.

My teacher told me to pump my brakes. He said that I skipped way too many steps.  Instead, he said, making a peanut butter and jelly sandwich involves a lot of prerequisite steps.

  • Collect all the ingredients
  • Open the jar of peanut butter
  • Open the jar of jelly
  • Insert a knife into the peanut butter
  • Spread the peanut butter onto the bread
  • Insert the knife into the jelly
  • Spread the jelly on the other slice of bread.
  • Combine the slices of bread.

That’s what programming is all about. 

You need to get into the weeds and break things into very specific step-by-step and unambiguous instructions sets that a computer can automatically execute.

These instructions map to how you write virtually all programs in most modern programming languages.

But not JavaScript. 

JavaScript pulls the “first you do this, then you do this” mentality, but sometimes it switches things up a bit. This is especially the case if it could take a while to perform the instructions. The JavaScript programming language also has no control over how long it takes.

Take the step:

  • Collect all the ingredients

This could take a while. Maybe you’re waiting for someone else to come back from the grocery store with the ingredients. If you do have to wait, it probably makes sense to do something else, but it’s important to finish making the PB&J sandwich eventually.

In this case, the steps turn into:

  • Start collecting the ingredients.
  • When the ingredients are all collected:
    • Open the jar of jelly.
    • Etc.

Remember to focus on the when!

The next advanced Javascript topic you need to understand

The next JavaScript topic that’s important to understand is something called an event queue.

Let’s say you’ve started many different actions. In this case, what happens if they all complete at the same time? 

In the previous example, what would happen if your oven timer goes off indicating that things are finished in the oven, but someone else comes home with the ingredients for a peanut butter and jelly sandwich at the same time?  

Returning to the analogy, as a person, you can only do one thing at a time. Should you sandwich or should you oven?

In JavaScript, this happens frequently, and it’s important that things always work consistently. 

The event queue is just a fancy way to explain that things should happen in the order they happen, which is a pretty logical approach. So, if the alarm went off first, finish dealing with the oven before making the sandwich. But if someone came back from the store with the peanut butter and jelly ingredients before the alarm went off, make the sandwich first.

Why there is hope for those who want to learn Javascript

JavaScript is a funky programming language. It’s different than nearly every other programming language out there. Once you get a feel for it though, it becomes more and more intuitive.

While foreign and strange at first, JavaScript solves problems in a truly elegant, intelligent, and even logical way. 

The very thing that makes JavaScript weird is the same thing that makes it powerful

It can feel uncomfortable to write JavaScript at first. But the more you use it, the more it becomes second nature. 

It’s no wonder that JavaScript is becoming more and more popular.

 

If you learned something from this post, share it on Twitter using the button below!

 

Recommended For You

One thought on “So, what exactly makes JavaScript such a weird programming language?

  1. Hi Ken,

    This was one of the BEST descriptions of JavaScript and it’s delicate intricacies! Thank you so much for posting! I guess I can consider myself one of the lucky ones, as I took a deep dive of JavaScript first, before learning any other programming language, so I am actually more comfortable with it.

    As I was working on the Ruby module in the prep course, I found myself getting frustrated, and saying “if I was using JavaScript, all I’d have to do is.. (fill in the blank)!” Funny how that works. I’m a newbie, and enjoying every second of it, frustrations and all! Thanks again and look forward to the next post!

Leave a Reply

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