How do we define JavaScript?

Defining what is JavaScript?

Posted by Rishikesh Shinde on Oct. 22, 2021, 3:12 p.m.
   
    1   
Responsive image

On the internet, we come across many definitions of JavaScript. But today we are going to define JavaScript differently.

I want to start with a definition that you might have learned, where it says that,

"JavaScript is a high-level, object-oriented, multi-paradigm programming language."

Now, this is of course 100% correct, but this is just a surface, the tip of the iceberg. So what about this definition,

"JavaScript is a high-level, prototype-based object-oriented, multi-paradigm, interrupted or just-in-time compiler, dynamic, single-threaded, garbage-collected programming language with first-class functions and a non-blocking event loop concurrency model. "

You might think is this some kind of joke, but I just packed as much information as possible about JavaScript into one unreadable sentence just for fun. But besides fun, this also gives us a great opportunity to unpack all of these concepts to get the first peek into all this stuff that we are going to learn to define JavaScript. I just want you to get the big picture before really getting started and also introduced some topics that are just good to know because getting the big picture before diving deep is in my opinion is amazing learning technique. So with that being said let’s unpack this now a little bit.

High-level programming language.

Starting with a high-level part as you might already know every program that runs on your computer needs some hardware resources such as a memory card and CPU to do with work.

Now, there are low-level languages such as C where you have to manually manage these resources. For example, asking the computer for memory to create a new variable. On the other side, you have high-level languages such as JavaScript and Python where we do not have to manage resources at all. Because these languages have so-called abstractions that take all of the work away from us. This makes the language easier to learn but the downside is the program will never be as fast or as optimised as the C program.

Now one of the powerful tools that take memory management away from us developers is garbage collection. This is basically an algorithm inside the JavaScript engine which automatically removes all unused objects from the computer memory in order not to clog it up with unnecessary stuff.

So it’s a little bit like JavaScript has a cleaning guy, who cleans our memory from time to time, so that we don’t have to do it manually in our code.

Interpreted or just in time compiled language.

Computer's processors only understand 0s and 1s, that’s right. Ultimately every single program needs to be written in zeros and ones which is also called machine code and since that is not really practical to write, is it? We simply write human-readable JavaScript code which is an abstraction over machine code. But this code eventually needs to be translated to the machine code and that step can be either compiling or interpreting.

This step is necessary for every single programming language because no one writes machine code manually. In the case of JavaScript, this happens inside the JavaScript engine.

Multi-paradigm.

Now, one of the things that makes JavaScript so popular is the fact that it's a multi-paradigm language. In programming, a paradigm is an approach and an overall mindset of structuring our code, which will ultimately direct the coding style and technique in a project that uses a certain paradigm. And this definition sounds kind of abstract. But don’t worry it will become more clear as we move on.

Now, three popular paradigms are, ProceduralObject-orientedFunctional programming.

Procedural programming is basically just organizing the code in a very linear way and then with some functions in between.

About object-oriented programming and functional programming I will talk about them in a second. Also, we can classify the paradigm as imperative or as declarative.

Now, many languages are only procedural or only object-oriented or only functional but JavaScript does all of it, so it's really flexible and versatile. And so we can do really whatever we want with it. It’s our choice we can use whatever paradigm we want.

So about the object-oriented nature of JavaScript, it is a prototype-based object-oriented language. What does that mean? Well, first almost everything in JavaScript is an object except for primitive values such as numbers, strings, etc. But arrays for example are just objects.

Have you ever wondered why we can create an array then use the push method on it? Well, it’s because of prototypal inheritance. Basically, we create arrays from an array's blueprint, which is like a template. This is called the prototype.

This prototype contains all the array methods and the arrays that we create in our code then inherit the methods from the blueprint. So that we can use them on the arrays and this, what I just explained to you is actually a huge oversimplification which might still sound confusing.

First class functions.

JavaScript is a language with first-class functions, which simply means that functions are treated just as regular variables. So, we can pass functions into other functions and we can even return functions from functions. And this is extremely powerful because it allows us to use a lot of powerful techniques and allows for functional programming which is one of the paradigms that we just talk about. And in fact, we use them without knowing that they are first-class functions. For example :

result.addEventListener('click', clickHandler);

In this example, `clickHandler is actually a first-class function that we use without knowing its name actually. And not all languages have first-class functions, but JavaScript has and it is amazing. Believe me, it's really really helpful.

Dynamic language.

Javascript is a dynamic language and dynamic actually means dynamically -typed. So, as we already know in javascript we don't assign data types to variables. Instead, they only became known when the javascript engine executes our code. Also, the type of variables can easily be changed as we reassign variables, and this is basically what dynamically-typed means.

The same is not true for most other languages, where we have to manually assign types to variables and this usually prevents bugs from happening which is the reason why many people say that javascript should be a strongly typed language as well. And if you yourself want to use javascript with type then you can always look into typescript.

Single-thread and non-blocking event-loop concurrency.

Let's now finally talk about the single-thread and non-blocking event loop concurrency model. Now, this is a really complex topic and probably the most complex one out of the whole javascript. So, I am not going to go deep but instead, I will just define some things here.

First, what is a concurrency model? Well, it's just a fancy term that means how the javascript engine handles multiple tasks happening at the same time. That's cool but why do we need that? Well, because javascript itself runs in one single thread which means that it can only do one thing at a time. And, therefore we need a way of handling multiple things happening at the same time and by the way, in computing, a thread is like a set of instructions that are executed in the computer CPU. So basically, the thread is where our code is actually executed in a machine's processors.

All right, but what if there is a long-running task like fetching data from a remote server? Well, it sounds like, that would block the signal thread where the code is running, right? But of course, we don't want that. What we want is so-called non-blocking behaviour and how do we achieve that? Well, by using the so-called event loop. The event loop takes long-running tasks executes them in the background and then puts them back in the main thread once they are finished.

And this is in a nutshell JavaScript non-blocking event loop concurrency model with a single thread.

It sounds like a mouthful for sure but in the end, it really just compresses to this.

Wrapping Up!

So to define what is javascript we saw these features of javascript

  1. High-level programming language.
  2. Interpreted or just in time compiled language.
  3. Multi-paradigm.
  4. First-class functions.
  5. Dynamic language.
  6. Single-thread and non-blocking event-loop concurrency.

So, I hope now you can not only be able to define javascript by yourself but also you can be able to elaborate on that definition using these features of javascript.

I know there is a ton of stuff to take in but I hope that you still learned a thing or two here.😊

Thank you.