How JavaScript Works

Photo by Mohammad Rahmani on Unsplash

JavaScript is often described as some variation of “high-level, single-threaded, garbage-collected, interpreted (or just-in-time compiled), prototype-based, multi-paradigm, dynamic language with a non-blocking event loop”. You may encounter these terms during a JS job interview, but understanding their meaning will help you better understand the behavior of your code. Let’s unpack each of these key terms.

High Level

High-Level refers to the abstraction the language provides over the machine’s bare-metal hardware. JavaScript is considered high-level because it does not require direct interaction with the operating system, hardware. In addition, it does not require memory management like C/C++ because the runtime always uses garbage collection.

Interpreted or Just-in-Time Compiled

Interpreted means the source code is converted to bytecode and executed at runtime (as opposed to being compiled to a machine code binary at build time). This is also why JS is commonly called a “scripting language”. Originally, it was only interpreted, but modern JS engines like V8, Spidermonkey, and Nitro use various techniques to perform Just-in-Time Compilation or JIT for better performance. Developers still use JS like an interpreted language, while the engine magically compiles parts of source code to low-level machine code behind the scenes.

Multi-Paradigm

Multi-Paradigm means the language is general-purpose or flexible. JS can be used for declarative (functional) or imperative (object-oriented) programming styles.

Dynamic Weakly Typed

Dynamic most often refers to the type system. JS is a dynamic weakly typed language, meaning you do not annotate variables with types (string, int, etc) and the true types are not known until runtime.

Prototypal Inheritance

Prototypal Inheritance means that objects can inherit behaviors from other objects. This differs from classical inheritance where you define a class blueprint for each object and instantiate it. We will take a deep dive into prototypal inheritance later in this course.

Event-Loop Concurrency Model

Single-Threaded means that JS can only run one instruction at a time, even if your CPU has multiple cores and available threads.

So this begs the question… How does JavaScript handle jobs at the same time (i.e. concurrently)?

Event Loop refers to a feature implemented by engines like V8 that allow JS to offload tasks to separate threads. Browser and Node APIs execute long-running tasks separately from the main JS thread, then enqueue a callback function (which you define) to run on the main thread when the task is complete. This is why JS is called non-blocking because it should only ever wait for synchronous code from your JS functions. Think of the Event Loop as a message queue between the single JS thread and the OS.

while (queue.waitForMessage()) {
queue.processNextMessage();
}

This is how JavaScript(JS) works!

I hope you found it informative.

See you soon with a new article!

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Jest and Enzyme in React

Register Now: https://www.kundaliniyoga.edu.in/s/store

JavaScript: Remove Duplicates From Sorted Array in Place

Front-end buried point — Error monitoring

Build Web-Scrapper using node.js

The simplest way to automate delivery of your React Native Application

How to Use Ternary Operators Effectively in JavaScript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sagarika Dalai

Sagarika Dalai

Software Engineer

More from Medium

How does JavaScript work?

Awesome JavaScript Resources

Object-oriented Programming in JavaScript

What is a JavaScript