Why JavaScript is Called a Lightweight Programming Language

November 11, 2020

What is JavaScript, and Where is it Used? 

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages, many non-browser environments also use it, such as Node.js, Apache CouchDB and Adobe Acrobat. JavaScript is a prototype-based, multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and declarative (e.g. functional programming) styles.

Also JavaScript is used in a great variety of systems, and environments; from browsers to mobile applications, from desktop applications to IoT solutions, from backend to frontend, even databases, JS can be used everywhere.

Fun fact: Even Stanford University changed their engineering study materials from Java or Pseudo code to JavaScript / TypeScript !

So, What Makes JavaScript Lightweight? 

There Aren’t Too Many Language Structs Available

JavaScript does not have too many language constructs that you can use for building your code. We have functions, different types of loops that can be used on different types of data structures, and built in objects like Math, Date, Array and so. Also statements (like if-else), and declarations (like const, let) are available for creating variables. On first glimpse this does not sound monumental, but these constructs can be used almost too freely, whichever use case we need to cover.

It’s Not Strongly Typed

JavaScript uses dynamic typing, so everything that you declare or assign, the interpreter tries to figure out, what should be the type of a certain variable. After figuring out what is the type of a variable, it assigns type specific methods to the variable inheriting from its Object Prototype Chain.

For example:

const text = "this is a text"; // typeOf text === 'string';
const number = 54; // typeOf text === 'number';
const isNiceBoolean = true; // typeOf text === 'boolean';

So from this point we can access type specific methods:

const textUpperCase = text.toUpperCase(); // returns ‘THIS IS A TEXT’
const textFromNumber = number.toString() // returns ‘54’;
const isThisReallyTrue = isNiceBoolean.toString(); // returns ‘true’;

If the interpreter can’t figure out the type of a variable, it most likely means we have an error in our code. For example we want to use a variable before it is declared, it will become undefined. Or we are mismatching a string for a number, it will become ‘NaN’ (not a number).

Advantages can be that we have incredible freedom in how we want to write our code. Some of this freedom makes really clever workarounds possible, offers solutions to common problems and what preferred solution we  choose. 

BUT this certainly does not provide protection against human error. 

JS devs can easily get stuck in a bug investigation for days because some method or variable was overridden to another type. That’s why TypeScript came… but that story is for another time. 😉 

Simple Language Build Blocks

In JavaScript everything is treated as an object, you can just declare a variable and access a lot of predefined methods right away. Or you can just create a function that processes and handles some data. Much less abstractions than other programming languages like Java or C#.
Devs can choose between an Object Oriented approach, or a functional declarative way to write code in this language.

Simple Syntax, Easy to Learn

Since JavaScript takes care of a lot of things under the hood, such as dynamic typing, starting programmers can focus on understanding the key concepts, and looking for best practices even in the beginning. With JavaScript, developers can focus on data structures, and best practices more, since the language is simpler in nature.

Common and Best Practices Are Easier to Pick Up

In the last decade JavaScript developed a lot, and developers figured out a lot of best practices that can be used writing an application. Performance and ‘Clean Code’ best practices and design patterns are applied more and more. Even these are heavily included in the design and architecture of modern JavaScript frameworks such as Angular, React, Vue. Let’s see some examples that can be familiar:

// Constructor Pattern
function Car(model, year, miles) {
   this.model = model;
   this.year = year;
   this.miles = miles;
}

const bmw = new Car('M4', '2019', '1000');

// Module Pattern:
var testModule = (function() {
  var counter = 0;
  return {
    incrementCounter: function() {
      return ++counter;
    },
    resetCounter: function() {
      counter = 0;
    }
  };
})();

// Usage:
testModule.incrementCounter();
testModule.resetCounter();

High Level Language

JavaScript is less hardware close, it means that its core functions are handled in a lower level machine / engine (like memory allocation, pointers, garbage collection, or threading). Also its concurrency model the infamous “Event Loop” is also handled automatically. 

For most of the use cases developers regularly meet, they don’t really need to know what happens under the hood, unless they need to do hardcore performance optimization, or using async solutions.

Bonus: Why has JavaScript started to become less lightweight as time went by? 

Since the ES6 standard was implemented in the language, JS got expanded by several new features and constructs, and this process is making the language itself less lightweight. Browsers and technologies took the time to catch up with the new standards, but nowadays unless we need to support some really outdated systems or browsers, we can easily use these newly introduced elements and additions.

The advantages of this is that developers with the help of this updates can write better, faster, and cleaner code, also these new elements help broadening the possibilities of any business or customer requirements.

The disadvantage of this fast paced evolution is that a lot of projects and technologies can be outdated, and it is never easy to migrate legacy code. Also it takes a large amount of effort to keep up all the new solutions engineers provide.

Frameworks like Angular, React, Vue also have the same effect, but keep in mind without the lightweight nature of JavaScript, these frameworks could not really exist as it is.


Ádám Ferenczi
Lead Developer