What is JavaScript?
Introduction
From the technological perspective, JavaScript is a programming language that:
-
has a C-like syntax,
-
is high-level,
-
is weakly typed,
-
features prototype-based inheritance (aka prototype-based object-orientation),
-
features class-based inheritance (aka class-based object-orientation), and
-
supports multi-paradigm programming, including object-oriented, functional, imperative, and event-driven programming.
JavaScript Origins
In 1993, the NCSA Mosaic browser, developed at the National Center for Supercomputing Applications at the University of Illinois, was released. It became the first publicly used browser available on the Windows operating system that integrated text and graphics. It supported multiple Internet protocols such as Hypertext Transfer Protocol (HTTP) and File Transfer Protocol (FTP) but it did not feature a scripting language similar to JavaScript. Microsoft licensed one of the versions of Mosaic in 1995 and built Internet Explorer on top of that.
In 1994, the lead developers of Mosaic founded Netscape Navigator browser which quickly became prevalent. In 1995, it was decided to add a scripting language to Netscape Navigator to allow for dynamic modifications of documents. One of the ideas was to use the Java language in collaboration with Sun Microsystems, the other was to embed the Schema language. However, finally, it was decided to devise a new one. The task was entrusted to Brendan Eich.
The newly devised scripting language was called LiveScipt in one of the beta iterations of the Netscape browser but the name was changed to JavaScript for the official release. The first official version of the Netscape Navigator browser that featured JavaScript was released in December 1995.
Microsoft also released its browser, Internet Explorer, in 1995. Internet Explorer was equipped with JScript which interpreter was reverse-engineered JavaScript interpreter from Netscape Navigator. Thus, the march to the prevalence of JavaScript as the main scripting language of the web has begun.
JavaScript Ecma Standardization
Approximately one year after its official debut Netscape submitted JavaScript to Ecma International for standardization.
The first version of JavaScript standardized by Ecma International was called ECMAScript (ES1) and released in 1997.
The second standardized JavaScript version called ECMAScript 2 (ES2) was released in 1998.
The third standardized JavaScript version called ECMAScript 3 (ES3) was released in 1999.
In the beginning, Microsoft and its browser Internet Explorer adhered to the Ecma standardization but later its collaboration stopped. Given the prevailing market share of Internet Explorer at the time, this caused the fourth version of the standardized JavaScript version (ES4) to never be released.
Microsoft's dominance in the browser market of the early 2000s was broken due to the release in 2004 of Netscape Navigator successor, Firefox, and the release in 2008 of Google Chrome. Google Chrome's featured fast JavaScript runtime engine - V8. Both browsers bit huge chunks from the browser market. The need for standardization arose anew.
The joint effort of interested parties and Ecma International led to the release of epochal JavaScript versions, namely ECMAScript 5 (ES5) in 2009 and ECMAScript 6 (ES6) in 2015.
The subsequent versions of JavaScript are denoted by their release year rather than a number. Therefore, after ES6, the ECMAScript releases were called ECMAScript 2016, ECMAScript 2017, ECMAScript 2018, ECMAScript 2019, etc. Although, their numeric names, ES7, ES8, ES9, ES10, etc. are also sometimes used.
JavaScript as a Client-Side Programming Language
As outlined above, JavaScript originated as a user-agent (browser) built-in programming language.
Today, JavaScript is the primary and the most pervasive client-side language of the World Wide Web.
On the client-side JavaScript can be used among others to:
-
manipulate Document Object Model, i.e. to add, change, and remove elements of documents in browsers, and
-
fetch data from remote hosts.
To streamline JavaScript development on the client-side many libraries and frameworks were developed. Currently, amongst the most popular are:
JavaScript as a Server-Side Programming Language
Although JavaScript began its life as a client-side (browser embedded) scripting language it is also now vastly used as a server-side language.
The popularity of JavaScript as a server-side programming language began with the creation of Node.js in 2009 by Ryan Dahl.
Node.js uses JavaScript V8 engine developed by Google under the hood. Node.js features event loop and I/O APIs.
The default package manager for Node.js is called npm.
There are many server-side JavaScript libraries and frameworks that facilitate development. Some of the notable are:
JavaScript as a High-Level Programming Language
JavaScript is a high-level programming language.
JavaScript as a Just-In-Time Compiled Programming Language
JavaScript is a just-in-time compiled programming language.
JavaScript as a Weakly-Typed Programming Language
JavaScript is often referred to as a weakly typed programming language.
However, there are many different definitions of what a weakly typed programming language actually is.
When referring to JavaScript as a weakly typed language it is often understood as JavaScript capabilities of implicit casting of its values in some of its built-in operators.
For example, when comparing two values of different data types using abstract comparison ==
operator those values are implicitly cast to congruent data types before actual comparison.
1 == "1" // => true
Another example is an implicit casting of both operands to strings - when any of the operands is not a number - by the binary +
operator.
1 + "1" // => "11"
Yet another example is an implicit casting of both operands to numbers - when any of the operands is not a number - by the binary -
operator.
1 - "1" // => 0
JavaScript as a Dynamically Typed Programming Language
JavaScript is a dynamically typed programming language.
// JavaScript (dynamic typing)
let foo = 42
foo = "forty-two" // Re-assignment from number to string allowed.
// TypeScript (static typing)
let foo = 42 // foo data type implicitly statically set to type 'number'
foo = "forty-two" // Type 'string' is not assignable to type 'number'.
JavaScript as a Prototype-Based Programming Language
Since its beginning in 1995, JavaScript has been a prototype-based programming language.
JavaScript implements delegation-based prototyping.
const human = { profession: 'working', order: 'Primates' }
const student = { name: 'Dave', profession: 'studying' }
Object.setPrototypeOf(student, human)
student.name // => 'Dave'
student.profession // => 'studying'
student.order // => 'Primates'
Further reading at Prototypal Inheritance in JavaScript.
JavaScript as a Class-Based Programming Language
Since ECMAScript 2015 (ES6), JavaScript is a class-based programming language.
class Dog {
constructor(name) {
this.name = name
}
bark() {
console.log('Woof!')
}
}
const pluto = new Dog('Pluto')
pluto.name // => 'Pluto'
pluto.bark() // 'Woof!'
Further reading at Class-Based Inheritance in JavaScript.
JavaScript as an Object-Oriented Programming Language
JavaScript is an object-oriented programming language.
const pirate = {
name: 'Hook',
battleCry: function() { console.log('Huzzah!') }
}
pirate.name // => 'Hook'
pirate.battleCry() // 'Huzzah!'
However, JavaScript is not a purely object-oriented programming language (like for example Ruby) as it still features primitives (such as numbers, strings, and booleans) which have no state, nor methods associated with them.
JavaScript as a Functional Programming Language
JavaScript is a functional programming language.
When functions have capabilities of being bound to identifiers, passed as arguments, and being other functions return values, they are often referred to as the first-class citizens.
JavaScript can make use of two functional programming paradigms:
-
an impure functional programming, and
-
a pure functional programming.
An impure functional programming is a functional programming:
-
which can rely on out-of-scope values and therefore can return different values when provided the same arguments, and
-
in which function execution is allowed to have side effects such as out-of-scope value modification.
A pure functional programming is a functional programming:
-
which cannot rely on out-of-scope values and therefore always returns the same value when provided the same arguments, and
-
in which function execution is not allowed to have side effects such as out-of-scope value modification.
JavaScript as an Imperative Programming Language
JavaScript can be used as an imperative programming language.
let age = 42
const feelingYoung = true
if (feelingYoung) {
age = age / 2
}
JavaScript as an Event Driven Programming Language
JavaScript can be used as an event-driven programming language.
// Logs "window clicked" every time the browser window is clicked.
const logWindowClicked = () => console.log('window clicked!')
window.addEventListener('click', logWindowClicked, false)