The basics of syntax

INPUT · Slides

Meet the variable

01 / 05

A variable is a box with a name on it

Up to now you have typed values straight in. But if the same value comes up more than once, it is easier to give it a name and keep it somewhere.

That named box is what we call a variable.

02 / 05

Make one with let

To make a variable you write let, then the name, then =, then the value you want in it.

= does not mean "is equal to" here. It means "put the thing on the right into the thing on the left". That is different from the = in your maths book, so watch out.

let name = "Yui";console.log(name);

Result

Yui

03 / 05

Write the name and out comes what is inside

To use a variable you just write its name. No " around it.

Wrap it and you get "the word name" instead, and what is inside never comes out.

let city = "Tokyo";console.log(city);console.log("city");

Result

Tokyo
city

04 / 05

Numbers go in too

Strings are not the only thing a variable will hold. A number goes in just as happily, and once it is in there you can do maths with it.

let price = 150;console.log(price * 2);

Result

300

05 / 05

Choosing a name

You get to pick the name, but there are rules and there are habits.

  • You may use letters, digits, _ and $. It must not start with a digit
  • Pick a name that says what is inside (price beats a)
  • If it takes two words, the habit is to capitalise the second one, as in userName