Skip to guide content

Learn JavaScript: an interactive guide to the language

Understand what your code does

JavaScript is easy to start and surprisingly easy to misread. A copied object still changes. A function remembers a variable. A promise runs before a timer. The interesting part is understanding why.

A chapter-by-chapter JavaScript reference, from primitive values and operators to promises and modules. Read the notes, follow the code examples, and explore the interactive experiments.

Try one

Four lines of code, one surprising order. Step through them and watch where each callback waits before it runs.

console.log('A');setTimeout(() => console.log('timer'), 0);Promise.resolve().then(() => console.log('promise'));console.log('B');

Step 0 of 6: Guess the order the four logs will print, then press Next step.

Call stackrunning nowempty
Microtask queuepromises, runs firstempty
Task queuetimers, runs afterempty
Console
    Illustrative model. The controls step through the behavior described above.
    Figure 1. Execution order. Normal code runs first. Then promise callbacks (microtasks). Then timer callbacks (tasks).

    Chapters

    CHAPTER 2

    Functions

    Declarations, expressions, arrows, closures, and scheduling work with timers.

    CHAPTER 3

    Data structures

    Arrays, objects, sets, maps and strings, and when to reach for each.

    CHAPTER 4

    The DOM

    Selecting, creating and changing elements, and reacting to events.

    CHAPTER 5

    Object-oriented JavaScript

    Classes, constructor functions, prototypes and inheritance.

    CHAPTER 7

    Modern JavaScript and practice

    Modules, tooling, and the habits that make you a better developer.

    Glossary

    Closure
    A function together with access to the variables of the scope it was created in.
    Prototype
    An object consulted when a property is missing on another object.
    Hoisting
    Declarations are set up before code runs, so some can be used above the line that declares them.
    Coercion
    JavaScript converting a value to another type implicitly, as in "5" * 2.
    Callback
    A function passed to another function, to be called later.
    Promise
    An object standing in for a value that will be available later, or an error.
    Microtask
    Work, such as a promise reaction, processed after the current stack empties and before the next task.
    Scope
    The region of code where a variable can be accessed.
    Primitive
    An immutable value that is not an object: string, number, bigint, boolean, undefined, symbol or null.
    Event Loop
    The mechanism that takes queued tasks and runs them once the call stack is empty.
    Destructuring
    Unpacking values from arrays or properties from objects into separate variables.

    Further reading