30 Mins

1. Introduction to JavaScript

History and Evolution of ECMAScript

JavaScript was created in 1995 by Brendan Eich in just 10 days. However, to standardize the language across different web browsers, it was submitted to ECMA International, resulting in the official standard known as ECMAScript.

ECMAScript (ES) defines the rules, syntax, and fundamental objects of the language. JavaScript is the most popular implementation of this standard.

  • ES5 (2009): Introduced strict mode, JSON support, and array methods like forEach, map, and filter.
  • ES6 / ES2015: The most massive update in JS history. It introduced let and const, arrow functions, classes, template literals, destructuring, promises, and modules.
  • ES7+ (2016-Present): Yearly incremental updates adding features like async/await (ES8), optional chaining ?. (ES11), and nullish coalescing ??.

Browser Compatibility & Transpilation

Because older browsers may not support the latest ECMAScript features, developers often use tools like Babel to transpile (convert) ES6+ code back into ES5-compatible code to ensure universal execution.

JavaScript
// Legacy ES5 Syntax
var oldGreeting = "Hello World";
function legacyAdd(a, b) {
  return a + b;
}

// Modern ES6+ Syntax
const modernGreeting = "Hello, Modern JavaScript!";
const modernAdd = (a, b) => a + b;

How Engines Execute JS (V8, SpiderMonkey, JIT Compilation)

JavaScript engines like V8 (used by Google Chrome) and SpiderMonkey (used by Mozilla Firefox) play a critical role in executing JavaScript code. These engines use a technique called Just-In-Time (JIT) compilation to improve the performance of JavaScript. JIT compilation involves compiling frequently executed parts of the code into machine code on the fly, which significantly speeds up the execution process. This compilation happens in the background while the code is running, hence the term just-in-time.

JavaScript
var x = 5;
// Example variable declaration

Engines also employ various optimization techniques, such as caching, to minimize the time spent on compilation and maximize the time spent on execution.

Execution Environment: Browser Runtime vs Node.js Runtime

JavaScript requires a runtime environment to execute. The runtime provides the engine (like V8) and the environment-specific APIs.

The Browser Runtime

In the browser, JavaScript is used to manipulate the webpage and interact with the user. The browser runtime provides:

  • DOM API: (Document Object Model) to manipulate HTML elements (e.g., document.getElementById).
  • BOM API: (Browser Object Model) to interact with the browser window (e.g., window.innerWidth, navigator, location).
  • Web APIs: Features like fetch, setTimeout, and Web Storage.

The Node.js Runtime

Node.js is a runtime that allows JavaScript to run on the server. It uses the same V8 engine but provides different APIs since there is no HTML or browser window:

  • File System (fs): To read and write files directly to the OS.
  • HTTP/Network: To create web servers and handle network requests.
  • Process: The global process object provides information about the current Node.js process.
JavaScript
// Browser Runtime Only (Will throw error in Node.js)
if (typeof window !== "undefined") {
  console.log("Running in a browser!", window.document.title);
  localStorage.setItem("session", "active");
}

// Node.js Runtime Only (Will throw error in Browser)
if (typeof process !== "undefined") {
  console.log("Running in Node.js!", process.version);
  // require('fs').readFileSync('./data.txt');
}

Connecting External Scripts to HTML

To connect an external JavaScript script to an HTML document, you use the