The basics of syntax

INPUT · Slides

Doing some maths

01 / 05

Write a sum and it gets worked out

Put a sum inside the brackets of console.log() and the answer is what shows up.

When JS spots a sum, it works it out first and shows the result.

console.log(3 + 5);

Result

8

02 / 05

Plus, minus, times

The symbols are a little different from the ones you know, so learn them now.

  • add … +
  • subtract … -
  • multiply … * (an asterisk, not ×)

* is the symbol on your keyboard. The × from your maths book will not work.

console.log(10 + 4);console.log(10 - 4);console.log(10 * 4);

Result

14
6
40

03 / 05

A sum can be as long as you like

You can chain the symbols up. Multiplication happens before addition, exactly as it does on paper.

When you want a different order, wrap that part in ( ).

console.log(2 + 3 * 4);console.log((2 + 3) * 4);

Result

14
20

04 / 05

Spaces are up to you

A space either side of a symbol changes nothing. JS does not mind.

But it reads better with them in, so this course will always put them in.

You spend longer reading code than writing it. It is worth the effort.

console.log(3+5);console.log(3 + 5);

Result

8
8

05 / 05

Maths takes the lead role later

Right now you are typing the numbers straight in. From the next lesson you can give a value a name and do maths with that instead.

You will be able to write total = price * count — a sum you can actually read. Maths is the floor everything else stands on, so get comfortable with the symbols here.