What is JavaScript?

What is JavaScript?

The Ultra Pro Max definition of JavaScript

Definition

JavaScript is a high-level prototype-based object-oriented multiparadigm interpreted or just-in-time compiled dynamic single-threaded garbage-collected programming language with first-class functions and a non-blocking event loop concurrency model.

This definition is given by Jonas Schmedtmann

Hashhhhh🥵 That was hard to read in a single breath. Let's understand it by breaking it down into a few jargon.

dr-js.png

High-level

Whenever a program needs any resources, in a high-level language the developer does not have to worry about it, everything happens automatically. This is because these languages have an abstraction that takes away all the work from the developers. This makes the language easy to learn and use. Although these programs are not as fast and optimized as any low-level language like C.

high-level-meme.png

Prototype-based

Almost everything in JavaScript is an object. For any data structure that we use, for example, an array is built from its prototype(i.e. blueprint). Thus, it inherits all its methods like push and pop in the case of an array. According to Mozilla's documentation:

A prototype-based language has the notion of a prototypical object, an object used as a template from which to get the initial properties for a new object.

Object-oriented

The basic idea of Object-Oriented Programming is that we use objects to model real-world things that we want to represent inside our programs. E.g. of an object can be a car, animal, or anything thing. JavaScript can support OOP because it supports inheritance through prototyping also properties and methods. It even has polymorphism, encapsulation, and many sub-classing paradigms.

Multiparadigm

A paradigm in computer science is an approach and mindset of structuring code, which will direct your coding style and technique. Some examples of paradigms are procedural programming, object-oriented programming, functional programming, etc. JavaScript does all of these i.e. procedural programming, object-oriented programming, and functional programming thus it is very flexible.

Interpreted or Just-in-time compiled

The computers only understand machine code i.e. 0's and 1's which is in a way impractical to write. Thus, we write human-readable code which is an abstraction over machine code. Thus, it's needed to be converted to machine code which is down by compiling or interpreting. It happens inside the JS engine. Thus, in JavaScript, the compilation is done during execution. JIT helps in improving the performance of programs by compiling bytecode into native machine code at run time.

Dynamic

JavaScript is a dynamically typed language i.e. we do not assign datatypes to variables. Therefore, there are no datatype definitions for variables. Type becomes known at runtime. The datatype of the variable is automatically changed if we reassign variables.

Single-threaded

Javascript is a single-threaded language. This means that it has only one call stack and one memory heap. Thus, it executes code in a synchronous manner and finishes a statement before moving on to the next. It can sometimes be disadvantageous because it may block the program if some statement takes longer to execute.

Garbage-collected

It is an algorithm inside a JavaScript engine that automatically removes old unused objects from the memory. It can be said as the cleaning guy of JavaScript. Javascript automatically allocates memory when objects are created and frees it when they are not used anymore using the garbage collector. There are various garbage collecting algorithms like the Mark-and-sweep algorithm, Reference-counting-garbage-collection, etc.

garbage collection meme.webp

First-class functions

Functions are treated as regular variables in JavaScript. Thus, a function can be passed as an argument to other functions or even return from another function and can be assigned as a value to a variable. Weird, isn't it? That's the beauty of JavaScript😋

Event loop

The event loop's job is to look at callstack and callback queue and once the callstack becomes empty and there are some things in the callback queue, then it takes the first thing from the callback queue and pushes it into the callstack. With the help of the Event loop, JavaScript can do the asynchronous tasks even though it is single-threaded. Although event loop is not included in JavaScript rather it is the functionality of the browser and JavaScript has the non-blocking event loop concurrency model.

I hope this helps you learn and love JavaScript even more. See you soon with some other blog👋