Summary

In this lesson, you learned that:

  • A program is a list of statements that run one after another, from top to bottom. We end each statement with a semicolon ;.
  • console.log() prints to the console. Several values separated by commas are printed on one line, with spaces between them: console.log("Age:", 25) prints Age: 25.
  • Text (a string) is written in quotes, numbers without them.
  • The arithmetic operators are +, -, *, / and % (remainder). Multiplication and division are done before addition and subtraction; parentheses change the order.
  • Decimal numbers are written with a dot: 1.5. A number is even when its remainder after dividing by 2 (number % 2) is 0.
  • Comments are notes that the computer ignores: // up to the end of the line, /* */ over several lines. You can also use them to switch code off (comment it out).
  • Variables store values under a name: let score = 0;. You change the value with =, without let: score = score + 1;.
  • Names are written in camelCase (firstName), can't start with a digit or contain spaces, and uppercase and lowercase letters matter.
  • Constants (const) can't be changed: trying to do so ends with TypeError: Assignment to constant variable. Use const for values that don't change and let for the others.
  • + adds numbers and joins text. If one side is text, the result is text: "5" + 3 is "53". typeof tells you the type of a value: number or string.
  • Template literals in backticks insert values into text: `I am ${age} years old.`
  • An error message has a type and a description. SyntaxError means that the code is written wrongly, and nothing runs; ReferenceError means an unknown name; TypeError means something the value doesn't allow.

In the next lesson, Conditions, your programs will learn to make decisions: to do one thing or another, depending on the values of variables.

Instructions

Before you go on, try it all out on a small task: a receipt from a coffee shop.

Create a constant total with the price of two coffees and one cake. Calculate it from coffeePrice and cakePrice.

Using a template literal, print the last line of the receipt: Total: 11.5 EUR

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

Back

15/15

Next