Learn / Lesson 2 of 10
Variables and types
Give values a name, and learn the difference between numbers, text and true/false.
Words on this page, in plain English (7)Open if any word is new to you
- Variable
- A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
- String
- A piece of text, written between quotes. Each letter in it is a character.
- Boolean (true or false)
- A value that can only be
trueorfalse. Like a light switch: on or off. - Loop
- Code that repeats. A
forloop repeats once per item or a set number of times; awhileloop repeats as long as something stays true. - console.log
- Prints something so you can see it. The easiest way to find out what your code is really doing.
- Template
- The usual shape of a pattern's code. You fill in the parts that change for each problem.
- Bug, debugging
- A bug is a mistake in code. Debugging is finding it, often by printing values with
console.log.
Giving values a name
A variable is a name for a value, like a labelled box. You create one with let, give it a name, and put a value in it with =:
Try it: a variable
let score = 10;
console.log(score);
score = score + 5; // take the old value, add 5, store it back
console.log(score);Press Run to see the output. You can change the code first.
Read = as "becomes", not "equals". score = score + 5 means "score becomes whatever score was, plus 5".
let and const
There are two ways to create a variable:
letfor values that change (a counter, a running total).constfor values that never change after you set them.
Try it: const can't change
const name = "Ada";
console.log(name);
name = "Grace"; // this line breaks the rule
console.log(name);Press Run to see the output. You can change the code first.
Use const by default, and let when you know the value will change. It makes code easier to read: a const always means the same thing.
Names can use letters, digits, _ and $, but can't start with a digit. JavaScript programmers usually write names in camelCase: totalPrice, bestScore. Capital letters matter: score and Score are different variables.
Types of values
Every value has a type. The basic ones:
| Type | Examples | What it's for |
|---|---|---|
| number | 42, -7, 3.14 | counting, measuring, maths |
| string | "hello", 'a', "" | text, in quotes |
| boolean | true, false | yes or no |
| undefined | undefined | "no value yet" |
| null | null | "deliberately empty" |
typeof tells you the type of a value:
Try it: typeof
console.log(typeof 42);
console.log(typeof "42");
console.log(typeof true);
let nothingYet;
console.log(nothingYet); // created, but never given a value
console.log(typeof nothingYet);Press Run to see the output. You can change the code first.
Notice 42 and "42" are different types. One is a number, the other is text that happens to contain digits.
Doing things with values
Numbers use the maths operators you'd expect, plus one you might not: %, the remainder after dividing.
Try it: operators
console.log(7 + 2, 7 - 2, 7 * 2, 7 / 2);
console.log(7 % 2); // 7 ÷ 2 is 3 remainder 1
console.log(10 % 5); // divides exactly: remainder 0
console.log(Math.floor(7 / 2)); // round down: 3Press Run to see the output. You can change the code first.
% is very handy: n % 2 === 0 means "n is even".
With strings, + joins text together. Template strings, written with backticks, let you drop values straight into text with ${...}:
Try it: joining text
const first = "Ada";
const last = "Lovelace";
console.log(first + " " + last);
const age = 36;
console.log(`${first} was ${age} years old.`);
console.log("5" + 3); // text + number joins them as text!Press Run to see the output. You can change the code first.
That last line surprises everyone: "5" + 3 gives "53", because one side is text. If you need the number, convert first with Number("5").
Comparing values
Comparisons give a boolean, true or false:
Try it: comparisons
console.log(5 > 3);
console.log(5 < 3);
console.log(5 >= 5); // at least
console.log(5 === 5); // the same value
console.log(5 !== 4); // not the same
console.log(5 === "5"); // different types, so not the samePress Run to see the output. You can change the code first.
Always use === and !== (three characters). The two-character == tries to convert types first, which causes confusing bugs like 0 == "" being true.
You can combine booleans with && (and), || (or) and ! (not):
const age = 20;console.log(age >= 18 && age < 65); // both must be trueconsole.log(age < 13 || age > 65); // either one is enoughconsole.log(!true); // the opposite: falseYour turn
A shop sells notebooks for 3 each. Make a const for the price and a let for how many someone buys. Show the total, then change how many they buy and show the new total.
Your turn
const price = 3;
let count = 4;
// Show the total, then change count and show it again.Press Run to see the output. You can change the code first.
What you learned
letcreates a variable that can change;constone that can't.=means "becomes".- Values have types: number, string, boolean, undefined, null.
%gives the remainder.+adds numbers but joins strings.- Compare with
===,!==,<,>, and combine with&&,||,!.