The Definition of JavaScript
JavaScript as per definition is a high-level, single-threaded, garbage-collected, prototype-based, multi-paradigm, dynamic language with a non-blocking event loop, also lately known as the de-facto standard language of the web.
Oh 😶!! pretty much got over my head like a bouncer!!!
Don’t worry, we will dig out the definition in more detail here.
Let’s get into its short history at first!
JavaScript was developed by Tim Berners-Lee in 10 days !! Literally 10 days ! Those days in 90’s Netscape Navigator browser was evolving but everyone felt the need of having browsers more dynamic. Then the first version of JavaScript appeared called Mocha(syntactically it contained some features of Java- the most famous language at that time) but still possessed many of the features of modern JavaScript. Then subsequently the language was renamed to LiveScript and then later JavaScript. I will get into more detail in the evolution of the modern Javascript in next article, for now let’s not get too much deviated from our main topic 😀.
When we run a JavaScript program, be it either a web app or a Nodejs app, our computer needs to store the memory required for the variables and functions of our program as well as for code execution to Random Access Memory(RAM) and also allocate a CPU thread needed for the program to run. But as a JavaScript developer, we really don’t have to worry about any of these things as JavaScript is a high level language. It means JavaScript lies certain level high in the abstraction. Our computer understands the low degree of abstraction language with numeric codes only i.e. machine code. Then comes the assembly language that lies above the machine language in the abstraction hierarchy and then comes the high level language.
JavaScript code is run in a container called Execution Context. We can think as Execution context as a large box that contains all the JavaScript code we write. Execution context primarily is divided into two parts- Variable Environment (or memory where the variables and function declarations of our code are stored in the form of key value pairs) and Execution thread ( where all others code are stored and where the synchronous task of the JavaScript code is executed). JavaScript is synchronous i.e. the code at the top is always executed at first which is followed by the subsequent code below the top.
JavaScript is a single threaded language. Let’s take a quick example on this:
Open a tab in chrome, open a site lets say, https://medium.com/@shivaaryal, open the browser console(ctrl + shift + i), and paste the below code there:
while(1){ console.log("I m inside while loop") }
and hit enter. Now click anywhere on the main page.
Worrying about your chrome getting hang, also your PC gets slightly slow too, we can’t click anything on the main page of the chrome. Now go to settings from the menu on the top right corner, open task manager and see the tab where you just run the console thing, click and end the process for that tab. What exactly just happened?
It’s because the single thread of JavaScript is stuck in that while loop and can’t get outside of that thread and run other things. The only way to run other things is to get out of that single while loop thread.
JavaScript allocates memory automatically when things like strings, functions, objects, etc. are created rather than explicitly being allocated as in languages like C with functions like malloc(). This convention enables user not to worry much about the allocation as well as freeing the memory when the block of allocated memory is no longer required and then reclaim it. This feature of Javascript makes it a garbage-collected language.
Despite containing class and their corresponding instance as in class-based language, JavaScript is a prototype-based language. A prototype-based language simply has objects rather than class and instance. Here, classes are not explicitly defined, but rather derived by adding properties and methods to an instance of another class or, less frequently, adding them to an empty object. It has a prototypical object, an object used as a template to get initial properties for a new object. Any object can share it’s property and become a prototype to a new object.
A multi-paradigm language is a programming language that supports many programming paradigms, programmers can work in a variety of styles, freely intermixing constructs from different paradigms to allow programmers to use the best tool for their required job.
JavaScript supports both object-oriented programming with prototypical inheritance(imperative programming paradigm) as well as functional programming(declarative programming paradigm). That's why, JavaScript is considered a multi-paradigm language.
(learn more about programming paradigms here https://www.geeksforgeeks.org/introduction-of-programming-paradigms/ ).
A dynamic programming language is a programming language which at runtime execute many common programming behaviors that static programming languages perform during compilation. These behaviors could include an extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system. The word ‘dynamic’ here can denote a whole lot of things. In JavaScript, it is possible to change the type of a variable or add new properties or methods to an object while the program is running as JavaScript is a dynamically typed language( we don’t have to strictly declare the data type of a variable) though not all dynamic languages are dynamically typed ones.
These days, it seems like JavaScript is everywhere. From the static sites and applications that now characterize large swaths of the web to the compile-to-native frameworks now popular at enterprise organizations well beyond their original creators, JavaScript is increasingly responsible for a growing proportion of web and mobile experiences. JavaScript is gradually overtaking other programming languages to become the lexicon not just of the web but also of digital experiences in general, irrespective of whether they rely on web technologies or not.
Whether we like working with JavaScript or not, in the coming decades, we will be writing JavaScript to build experiences beyond the web too.