Basics in JavaScript

Basics = the building blocks: values, operators, choices and loops.

This chapter has 5 lessons and 23 exercises you run and check in your browser. Plan on about 102 minutes.

  • Introduction: what JavaScript is, your first console.log(), and adding scripts to a page.
  • Data Types: let and const, the kinds of values, and turning one type into another.
  • Operators: maths, comparisons, &&/||, the ternary and which operator runs first.
  • Conditional Statements: if, else if, switch and the ternary for making choices.
  • Loops: for, while, for...of, for...in, break and continue.

After this chapter, you can:

  • Store values in variables and pick the right type for each one.
  • Work out totals and compare values with operators.
  • Make the program choose between paths with if and switch.
  • Repeat work over a list with loops, and stop early when needed.

Every lesson has short explanations, code boxes you edit and run in the live editor, and a web page example you can click.

Next chapter: Functions.

Lessons in this section

  1. Introduction

    What JavaScript is and why it matters, how HTML, CSS and JavaScript share a page, your first program with console.log(), client-side versus server-side code, and adding scripts to HTML.

    • Explain what JavaScript is and why it is worth learning
    • Describe the roles of HTML, CSS and JavaScript on a page
    • Write a Hello World program and read its output
    • Tell client-side JavaScript from server-side JavaScript
  2. Data Types

    Declaring variables with var, let and const, the seven primitive types and the non-primitive object, array and function, and how implicit and explicit type conversion work.

    • Declare variables with let and const, and know why var is avoided
    • Name the seven primitive types and the non-primitive types
    • Check a value's type with typeof, including its two quirks
    • Predict the result of implicit type conversion with + and in conditions
  3. Operators

    Arithmetic, assignment, comparison, logical, unary and ternary operators, and the precedence rules that decide which operator runs first.

    • Calculate with the arithmetic operators, including % and **
    • Update variables with = and the shorthand assignment operators
    • Compare values with === and !==, and explain how == differs
    • Combine conditions with &&, || and !
  4. Conditional Statements

    Making decisions with if, if...else, else if and nested if, matching one value against many cases with switch, and producing a value with the ternary operator.

    • Run a block only when a condition is true with if
    • Choose between two blocks with if...else
    • Order an else if chain so the right branch wins
    • Flatten nested if statements with guard clauses
  5. Loops

    Repeating work with for, while and do...while, walking objects with for...in and arrays with for...of, and controlling a loop with break and continue.

    • Repeat a block a known number of times with a for loop
    • Repeat until a condition changes with while
    • Run a block at least once with do...while
    • Walk through the keys of an object with for...in