Skip to content

The Beginner's Guide: Single Threaded & Non-Blocking JS

Posted on:February 8, 2023 at 03:42 AM

Javascript is a single-threaded language that can be non-blocking.

What?!

In Zero To Mastery’s Complete Web Developer Course, I’m learning that this sentence needs to be understood by Javascript developers.

So what does single-threaded actually mean?

Javascript engines (which run our code in the browser) consist of the memory heap and call stack.

The memory heap is where memory is allocated for our code (e.g., each time we create a variable), while the call stack is where our code is read and executed.

Singled-threaded means that Javascript only has one call stack. Having one call stack means it only runs one thing at a time. This can simplify things compared to multi-threaded languages ( that have multiple call stacks), which can add complexity.

And what about non-blocking?

Javascript can be non-blocking. Essentially, Javascript says, “Ain’t nobody got time for that!” Javascript doesn’t wait around for things that take time, so it performs some tasks asynchronously.

In addition to the Javascript engine, there is also the Javascript run-time environment. This checks a few areas (Web APIs, the callback queue, and the event loop) to ensure the code runs efficiently and quickly. It also consistently checks the call stack to see if it’s empty or if there is still code that needs to be executed.

Whew! That took me longer than I want to admit to understand. I’m still working on fully understanding the run-time environment, but I’m closer 😊

Javascript is a single-threaded language that can be non-blocking.

Is it something you understood right away, or did it take time?