Introduction to Javascript

Introduction to Javascript

The Journey Begins

This is the first of a series of blogs, that'll cover the journey of learning the most ubiquitous skill of our times, a programming language that has a monopoly over the web, is running an industry worth hundreds of billions and is employing thousands of people - Javascript.

What is Javascript?

The Mozilla Developer Network (MDN) describes Javascript as follows:

"Javascript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and pretty much everything else. (Okay, not everything, but it is amazing what you can achieve with a few lines of JavaScript code.)"

More technically, if I were to describe Javascript, it'd be a programming language that has the following features:

  • High Level

  • Just-In-Time Compiled

  • Garbage Collected

  • Multi-Paradigm

  • Supports First Class Functions

  • Single-Threaded

Whoa, that's a lot to unpack! But don't worry I'll be elaborating upon each of them through the course of this write-up.

Features of Javascript

  1. A High-Level Language:

    Like most other programming languages - C, C++, Java, Python - Javascript is also a high-level language i.e. it can be comprehended by humans, but not by the machine, which only understands binary sequences. Of course, there is a hierarchy even among the high-level languages, in terms of their proximity to the machine.

    For instance, among the above-listed languages, C is considered to be the closest to the machine because the management of resources like the memory and the CPU, is to be done by humans, while in the case of Javascript it is handled by the language itself.

  2. Just-In-Time Compiled:

    There has been considerable debate over whether Javascript is a compiled language or an interpreted one. With the majority being of the opinion that like other 'scripts', JS is also an interpreted language. However, the truth is not so simple(it never is with JS, as we shall see in future write-ups).

    Javascript runs on a 'parsing model', where the code is simply read by the parser line by line, from start to finish. Note that no execution happens at this stage. If the parser finds no obvious syntax error(e.g. the declaration of the same variable twice) in the code, it generates what's called an Abstract Syntax Tree(AST, more on that later). Finally, just before the execution, an executable is generated from the AST.

    Thus it can be argued that Javascript, is in spirit closer to compiled languages than interpreted ones.

  3. Garbage Collected:

    Unlike in C and C++, garbage collection - i.e. removal of unreferenced objects and freeing up of memory - is handled by Javascript's internal garbage collector. This makes life a lot easier for developers (don't trust me? Talk to some C developers! )

  4. Multi-Paradigm:

    Javascript's syntax allows the developers to follow the major paradigms:

    • Procedural: Top-Down Approach

    • Object-Oriented: Structured around objects

    • Functional: Using blocks of functions, and use of those functions as values.

  5. Supports First Class Functions:

    As explained above, Javascript allows functions to be passed as values to other functions, and functions to be returned from other functions. This is one of the most important features of Javascript, which promotes a clean, succinct and logical style of coding.

  6. Single-Threaded:

    Javascript is a single-threaded language, i.e. it can perform only one task at a time. This can become a problem where there is some long-running task, such as fetching data from a remote database, and this task blocks all other small tasks until its completion. As a workaround to this bottleneck, Javascript uses what's called a "Non-blocking Event Loop" - This loop forces long-running tasks to the background, and puts them back on the main thread once the operation is completed. This is achieved through Asynchronous Methods and callback functions.

Conclusion

I hope I have let you see the tip of the iceberg that we are soon going to be exploring through the subsequent editions of these blogs. Catch you on the other side!

Chapter 2