JavaScript

JavaScript was written by Brendan Eich at Netscape in 1995 to script web pages. It borrowed first-class functions from Scheme and prototype-based objects from Self, and wrapped them in a C-like syntax. That mix explains why it supports so many styles.

Paradigm: Popular Hybrid Languages

What Makes JavaScript Special

  • Prototype-based objects. An object can inherit directly from another object with Object.create. There is no need for a class first.
  • Classes as syntax. The class keyword (ES2015) is a cleaner way to write prototypes. Private fields such as #n were added later.
  • Closures and higher-order functions. Functions capture their surrounding variables, so (x) => (y) => x + y builds new functions on demand. map, filter and reduce are built in.
  • Event loop and promises. One thread runs your code and a queue delivers events. Promise and async/await write asynchronous steps in a sequential style.
  • Dynamic and forgiving. Types are checked at run time and values convert automatically, which is flexible but a source of surprises. TypeScript adds a static type layer on top.

Example: Prototypes, classes, closures and the event loop

// 1. Prototype-based objects
const animal = { speak() { return this.name + " makes a sound"; } };
const dog = Object.create(animal);
dog.name = "Rex";
console.log(dog.speak());                    // Rex makes a sound

// 2. A class with a private field
class Counter {
  #n = 0;
  next() { return ++this.#n; }
}
const c = new Counter();
c.next();
console.log(c.next());                       // 2

// 3. Closures and higher-order functions
const makeAdder = (x) => (y) => x + y;
console.log([1, 2, 3].map(makeAdder(10)));   // [ 11, 12, 13 ]

// 4. Asynchronous code on the event loop
const wait = (ms) => new Promise((done) => setTimeout(done, ms));
(async () => {
  await wait(100);
  console.log("later");
})();
console.log("first");                        // printed before "later"

How It Works

  • dog has no speak of its own. The lookup follows its prototype link to animal, and this is still dog.
  • Counter hides #n from outside code, so only next() can change it.
  • makeAdder(10) returns a function that remembers x = 10. map applies it to every element without a loop.
  • await wait(100) pauses only the async function. The main script keeps running, so "first" prints before "later".

History and Where It Is Used

JavaScript runs in every web browser, and with Node.js it also runs servers, command-line tools and build systems. React, Vue, Svelte and Angular are all JavaScript (or TypeScript) frameworks. The language is standardized as ECMAScript by Ecma International, and a new edition appears every year. This site has full roadmaps for JavaScript and TypeScript.

Learn More