JavaScript Fundamentals

The JavaScript behind every web page: values, functions, closures, objects, classes, the event loop, promises and the DOM. Every lesson runs a real program line by line and shows its variables, its call stack and its objects.

JavaScript · beginner · 25 lessons · 2 h 43 min · free

What you'll learn

Lessons

The Basics

Variables, types, equality, strings, conditions and loops.

  1. Variables: let and const: Store values with let and const, change them, and see what a variable holds before it is given a value.
  2. Values and Types: Numbers, strings, booleans, null and undefined, checked with typeof. Then see what + and * do when a string meets a number.
  3. Equality, Truthy and Falsy: == converts before comparing, === does not. And every value counts as true or false in an if: see which ones are falsy.
  4. Strings and Template Literals: Build text with template literals and use the most common string methods.
  5. if, else and the Ternary: Choose between branches with if / else if / else, and pick a value with condition ? a : b.
  6. Loops: for, while and for…of: Add up an array with a counting for loop, count down with while, and stop a for…of loop early with break.

Functions

Calls and the stack, arrow functions, scope, closures, recursion and callbacks.

  1. Functions and the Call Stack: One function calls another. Watch each call get its own variables, wait for the inner call, and get its value back.
  2. Arrow Functions, Defaults and Rest: Short arrow functions, a default parameter for when an argument is missing, and ...rest to accept any number of arguments.
  3. Scope and Hoisting: Where a variable can be seen: the whole file, one function, or one block. And why var behaves differently from let.
  4. Closures: A function returned from another keeps the variables it was created with. Two counters each keep their own count.
  5. Recursion: factorial calls itself with a smaller number until it reaches 1. Watch the call stack grow four deep, then unwind.
  6. Functions as Values: Callbacks: Pass a function into another function and let it decide when to call it. Use a named function, then an arrow.

Objects and Arrays

Arrays and their methods, objects, destructuring, classes, this, Map and Set.

  1. Arrays and References: Add and remove items, then see that assigning an array copies the reference, not the array. Spread makes a real copy.
  2. map, filter, reduce and find: Transform, choose, total and search an array of orders. Each method calls your arrow once per item.
  3. Objects: Read, change, add and delete properties, give an object a method, and see two names share one object.
  4. Destructuring and Spread: Unpack values from objects and arrays into variables, rename and default them, and build new objects with ...spread.
  5. Classes and Inheritance: Make objects from a class with a constructor and methods, then extend it with a subclass that adds a method.
  6. How this Works: this depends on how a function is called, not where it is written. Lose it by taking the method out of its object, then fix it with bind.
  7. Map and Set: Collect unique words in a Set and count them in a Map.

Asynchronous JavaScript

The event loop, promises, async/await and errors.

  1. The Event Loop: Why does a 0 ms timer print last? Follow the call stack, the promise queue and the timer queue.
  2. Promises: Wrap a timer in a promise, then chain .then calls so each step starts when the previous one finishes.
  3. async and await: Write asynchronous code that reads like normal code. await pauses only its own function; errors arrive in catch.
  4. Errors: throw, try, catch, finally: Throw an error when input is bad, catch it further up, and run cleanup code in finally either way.

The Page

Changing the page and reacting to clicks.

  1. Changing the Page (the DOM): Find elements with querySelector, create new ones and add them to the page. The Screen panel shows the page change line by line.
  2. Events: Reacting to Clicks: Register a click listener, type a name and click: the browser calls your function, which reads the input and updates the page.

Related courses