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)printsAge: 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) is0. - 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=, withoutlet: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 withTypeError: Assignment to constant variable.Useconstfor values that don't change andletfor the others. +adds numbers and joins text. If one side is text, the result is text:"5" + 3is"53".typeoftells you the type of a value:numberorstring.- Template literals in backticks insert values into text:
`I am ${age} years old.` - An error message has a type and a description.
SyntaxErrormeans that the code is written wrongly, and nothing runs;ReferenceErrormeans an unknown name;TypeErrormeans 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