Javascript, What Are You?

Javascript, What Are You?

By: Raji Ayinla, Codesmith Intern

[Comic] Javascript, What Are You?

The Scenario

You’re sitting at a job interview at WECode.js that a staffing agency like Codesmith Staffing has set up for you, Mr. Javascript Developer.

The hiring manager clears her throat after glancing at your exquisitely detailed resume and asks, “What is Javascript?”’

“A single-threaded non-blocking asynchronous concurrent language,” you answer.

“Yes, but what does that mean and how would you explain that to, say, someone on the UX team?”


To avoid the awkward blank stare followed by more computer science buzzwords, we’re going to break down exactly what Javascript is. If you’re Mr. Javascript Developer or a newbie who’s intimidated by the notion of Javascript callbacks, everything will be demystified by our web comic. But before that, let us unpack Mr. Javascript Developer’s first answer.

What is a single-threaded language?

Simply put, a single threaded language uses a single call stack, meaning that it can only fire one event at a time. If you’re not exactly sure how to conceptualize a call stack, just picture a Jenga stack.

Say you’ve written a list of functions. Javascript’s runtime would then compile the script you’ve written and sequentially stack the functions like blocks within its call stack. Afterwards, each function that executes is taken from the top of the stack until it reaches the bottom of the stack. There is only one stack. This, ladies and gentleman, is single-threading.

Note: If you’ve been wondering what all this talk about V8 is, well now you know. It’s a call stack and a heap (memory allocation) that is specific to the chrome browser. There are several others. For example, Firefox’s engine is Rhino.

What the heck is non-blocking and asynchronicity and concurrency?

Well, let us try to understand what blocking is first. In a nutshell, blocking is the traffic caused by too many synchronous calls. Imagine a narrow exit that funnels drivers into a single lane road. That road is guarded by a traffic police who only allows one car at a time to go beyond his stop sign.

Now imagine the headache you’d have if you’re the fifteenth car in line and you have to get to the emergency room. Similarly, websites coded with blocking code make for a clunky UI with images that load slower than a snail can run.

So we understand what blocking is. Non-blocking is the complete opposite of blocking. Instead of making synchronous calls, a non-blocking language like Javascript uses asynchronous calls, or, in other words, an emergency lane that allows for a breakup of sequential order so that you can get to the hospital on time.

Here’s where things get really interesting.

By wrapping a javascript function within a Web API function like setTimeout, we can let the “secret underworld” of the browser handle this function without forcing it onto Javascript’s runtime. This works because Web APIs are independent of Javascript’s runtime. This concept of having a third party handle code is what concurrency is all about. We use callbacks to make our programs dynamic and prevent the event loop from clogging up.

We’ve mentioned Web APIs and an event loop. There’s also a callback queue. If you’re getting slightly confused, don’t panic. The comic will explain everything.


Javascript runtime: a single customer service call operator. He reads from a script, makes the call, and files it onto the stack.

Web APIs: a third party data store that holds a message for however long it is instructed to hold it.

The callback queue: a loading dock for calls

The event loop: a little green droid with one simple job–check if the stack is empty. If it’s empty, take an event from the queue and place it onto the stack. Continue to do so while the condition is true.

The Comic

As you can see from the very synchronous day, the data flow is very restricted. The “call operator” or the Javascript runtime has to manually log all of the calls. He has no time to get to the more important functions, like calling his wife or rendering a web page.

On a very asynchronous day, all the call operator has to do is load all of the Web API functions, then he can leave the task to both the Web API and the mini droid aka the event loop. The Web APIs store the callbacks until they are ready to be staged onto the call back queue. All the event loop (mini droid) has to do is check if the stack is empty and place the callback onto the stack. Meanwhile, the call operator can focus on more important functions, like dinner with the wife.


You can see why Javascript devs make a big fuss about callbacks. No one wants their Javascript runtime to be preoccupied with too many tasks. You can think of callbacks as Gwen Stefani’s hollaback girl. Javascript’s runtime sends a shout out to the Web APIs and performs other tasks while waiting for the hollaback, or, in our case, a callback.


Philip Robert’s awesome talk at JSConf EU 2014:

Vector images were Designed by Freepik

Write your comment Here