JavaScript as a Language

This section is an overview of JavaScript’s main characteristics. Some of it might not make sense yet, and that is fine. It is here in the preliminaries so that the whole picture is in one place. We will cover each topic in detail in the lectures to come.

Multi-Paradigm

JavaScript does not enforce a single programming style. You can write code using multiple paradigms:

Procedural

Breaking a problem into smaller parts and running the code step by step, in order. It also means organizing code into reusable procedures (functions) that you call when you need them.

function sum(numbers) {
  let total = 0;
  for (let i = 0; i < numbers.length; i++) {
    total += numbers[i];
  }
  return total;
}

let numbers = [1, 2, 3, 4, 5];
console.log(sum(numbers));

Object-Oriented

Organizing code around objects with data and behavior:

class Rectangle {
  constructor(width, height) {
    this.width = width;
    this.height = height;
  }

  area() {
    return this.width * this.height;
  }
}

Note: JavaScript’s object-oriented model is prototype-based, which differs from the classical inheritance found in Java or C++. The class syntax above is actually syntactic sugar over prototypes. We will explore this distinction in the chapter on Object-Oriented Programming.

Functional

Treating functions as values and avoiding side effects:

const total = numbers.reduce((sum, n) => sum + n, 0);

Most JavaScript codebases blend paradigms, using objects for structure and functional patterns for data transformation.

Dynamic Typing

Variables in JavaScript do not have fixed types. A variable can hold any value:

let x = 42; // x is a number
x = "hello"; // now x is a string
x = [1, 2, 3]; // now x is an array

Types are checked at runtime, not compile time. That gives you flexibility, but it also means a type error only shows up when the code runs.

Implicit Coercion

JavaScript performs type coercion. It converts values between types on its own:

"5" + 3; // "53" (number coerced to string)
"5" - 3; // 2 (string coerced to number)
true + 1; // 2 (boolean coerced to number)

Use strict equality (===) instead of loose equality (==) to avoid unintended coercion:

"5" == 5; // true (coercion happens)
"5" === 5; // false (no coercion, different types)

Garbage Collected

JavaScript automatically manages memory. You do not allocate or free memory manually:

let user = { name: "Alice" }; // memory allocated
user = null; // object becomes eligible for garbage collection

The garbage collector periodically finds memory that is no longer reachable and frees it. That prevents memory leaks from deallocations you forgot to write, but it also means you do not control when memory is freed.

Summary

Characteristic JavaScript
Paradigm Multi-paradigm (procedural, OOP, functional)
OOP Model Prototype-based (not classical)
Typing Dynamic, implicit coercion
Memory Garbage collected

In the next section, we will look at how JavaScript actually runs: engines, runtimes, and the event loop.