Chapter 3

JavaScript

JavaScript is the programming language of the web. It runs in every major browser and on servers through Node.js. It is what makes websites interactive: form validation, live search, loading data without a page refresh, animations, and everything else that happens after the page loads.

What this chapter covers

  • Basics : syntax, operators, and how to link JavaScript to an HTML page
  • Variables : const, let, and var, plus scope rules
  • Data Types : numbers, strings, booleans, null, undefined, and more
  • Functions : declarations, expressions, default parameters, closures
  • Arrow Functions : shorter syntax and how this behaves differently
  • Conditionals : if, else, ternary, switch, nullish coalescing
  • Loops : for, while, for...of, for...in
  • String Methods : includes, slice, trim, replaceAll, and more
  • Arrays : creating and iterating arrays, map, filter, reduce, sort
  • Objects : key-value pairs, destructuring, spread, Object.entries
  • Classes : constructor, methods, inheritance, private fields
  • Promises : async state, .then(), .catch(), Promise.all
  • Async/Await : writing async code in a synchronous style
  • Fetch API : making HTTP requests, handling responses, error handling
  • AJAX : what AJAX is and how fetch replaced XMLHttpRequest
  • DOM : selecting and manipulating HTML elements, event listeners
  • Modules : import, export, ES modules vs CommonJS
  • Errors : error types, try/catch/finally, custom errors

What version this covers

This chapter covers modern JavaScript, ES2015 (ES6) and newer. Where older patterns appear, they are labelled as legacy.

Prerequisites

You should be comfortable with basic HTML before working through the DOM section. Everything else only requires a willingness to type code and see what happens.

Try it in your browser

Every example in this chapter can be run directly in your browser’s developer console. Open it with F12 on Windows or Cmd+Option+J on Mac, click the Console tab, and start typing.