What is Javascript?
JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it’s used in many non-browser environments as well. It is a prototype-based, multi-paradigm scripting language that is dynamic, and supports object-orientation, imperative, and functional programming styles.
JavaScript runs on the client side of the web, which can be used to design / program how the web pages behave on the occurrence of an event.
JavaScript is a dynamic scripting language supporting prototype based object construction. The basic syntax is intentionally similar to both Java and C++ to reduce the number of new concepts required to learn the language. Language constructs, such as if
statements, for
and while
loops, and switch
and try ... catch
blocks function the same as in these languages (or nearly so).
JavaScript can function as both a procedural and an object oriented language. Objects are created programmatically in JavaScript, by attaching methods and properties to otherwise empty objects at run time. Once an object has been constructed it can be used as a blueprint (or prototype) for creating similar objects.
As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative programming styles. It has application programming interfaces (APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM)
Some of Javascript’s main characteristics are:
- High level
This refers to the level of abstraction the language provides regarding the hardware management of the computer. The lowest level of programming is in machine code, which is a language that can be directly executed by the CPU. Above that is assembly language which is a language designed for a specific type of processor. Then there’re programming languages like C that provide modern syntax and the chance to write multi platform programs, but the programmer still needs to program hardware management like with memory allocation. On the other side, with javascript and other modern languages like python the programmer doesn’t have to program hardware management and can just focus on the logic and execution of the program, in this working on a ‘higher level’ of abstraction.
- Interpreted
Javascript is an interpreted language because it needs to have an interpreter in it’s execution environment in order to translate javascript code into machine code, line by line. Other programming languages such as Java or C are compiled languages because previous to execution they’re compiled and translated directly into binary machine code, without the need of an interpreter.
Although Javascript is an interpreted language, some javascript engines like google’s v8 compile javascript to machine code previous to runtime, or just in time, in order to get better performance.
- Dynamically weakly typed
JavaScript supports dynamic typing which means types of the variable are defined based on the stored value. For example, if you declare a variable x
then you can store either a string or a Number type value or an array or an object. This is known as dynamic typing.
To understand this, in languages like Java, we explicitly mention that a particular variable will store a certain type of data, whereas in JavaScript we do not have to provide the data type while declaring a variable. In JavaScript, we just have to use var
or let
keyword before the variable name to declare a variable without worrying about its type.
Tools like Typescript come to reinforce this weakly typed characteristic of javascript.
- Multi paradigm
Javascript supports different programming styles like declarative functional programming or imperative object oriented programming, hence it’s called multi paradigm.
- Prototype-based Language
JavaScript is a prototype-based scripting Language. This means javascript uses prototypes instead of classes or inheritance. In languages like Java, we create a class and then we create objects for those classes. But in JavaScript, we define object prototype and then more objects can be created using this object prototype. Basically, everything within javascript is an object that inheritates properties and methods from other objects.
To understand this think about how an array has inherited methods like push, map or filter.
- Single threaded
This means the program can execute only one task at a time.
- With a non-blocking event loop…
The Event Loop refers to a feature implemented by engines like V8 that allow JS to offload tasks to separate threads and handle tasks that take longer without interrupting the running of the program. Browser and Node APIs execute long-running tasks separately from the the main JS thread, then enqueue a callback
function (which you define) to run on the main thread when the task is complete.
The event loop first reads and executes all the synchronous code and the reads if there’s any callback to execute.
- … and a garbage collector
The garbage collector is a javascript functionality that frees up the computer memory when it’s no longer referenced in the code. This means the programmer doesn’t have to free the memory explicitly when it’s no longer needed such as in other programming languages like C.
Cool resources about this topic: https://www.youtube.com/watch?v=FSs_JYwnAdI&ab_channel=Fireship