Values: Types, Operators and Math
- 5 min reading
- 6 min activity
0 of 5 tasks doneup to 95 XP
Mission 4, step 3. Programs work with values: names, numbers, yes and no. Today you learn the kinds of values and how JavaScript calculates with them.
- Create variables with
letandconst. - Tell the types of values apart and check them with
typeof. - Calculate with operators, join text with
+and useMath.
In lesson 4.2 you wrote const title = document.getElementById("title");. What did const do there?
Quick recall
Variables
A variable is a box with a name for a value. let makes a box whose value can change. const makes a box that keeps its value. Older code uses var; you do not need it.
let age = 25; // can change: age = 26;
const name = "Ayla"; // stays "Ayla"
Types of values
The type is the kind of a value. The operator typeof tells you the type: typeof 5 gives "number".
| Type | Examples | Meaning |
|---|---|---|
| string | "Ayla", "0000 000" | Text in quotes, also digits in quotes |
| number | 25, 3.14, -5 | A number without quotes |
| boolean | true, false | Yes or no |
| undefined | let box; | No value yet |
| null | let wallet = null; | Empty on purpose |
| object | { name: "Ayla", age: 25 } | Named values in one box; read one with person.name |
Good to know: typeof null gives "object". It is an old mistake in JavaScript that was never changed.
Which type?
Operators
+ - * /work as in maths.%gives the remainder:10 % 3is 1. An even number has remainder 0 when you divide it by 2.++adds 1,--takes 1 away.score += 10meansscore = score + 10;-=takes away.
let score = 0; // create it first
score += 10; // 10
score++; // 11
The slide uses score += 10 without creating score, which stops with an error. With text, + joins:
console.log("Lena" + " " + "Weber"); // Lena Weber
console.log("5" + 5); // 55: one value is text
Text plus number
Now be a type detective. This exercise counts for the Console Detective badge.
Type detective
Find out the type of four values with typeof. Then join the city and the number in one sentence.
The Math object
| Code | Result |
|---|---|
Math.round(4.6) (nearest whole number) | 5 |
Math.ceil(4.1) (always up) and Math.floor(4.9) (always down) | 5 and 4 |
Math.min(10, 5, 20, -3) and Math.max(10, 5, 20) | -3 and 20 |
Math.abs(-50) (without the minus) | 50 |
Math.pow(2, 3) and Math.sqrt(16) | 8 and 4 |
Math.random() | A number from 0 up to 1, never 1, for example 0.8234 |
Worked example: the hypotenuse
The slides use Pythagoras: in a triangle with a right angle, a² + b² = c². With a = 3 and b = 4, c is 5.
- Store the sides
let a = 3;andlet b = 4; - Calculate and store
Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)). Put the result into a variable, or it is lost. - Print
console.log("Hypotenuse: " + c);
The slide's code misses one thing in step 2. Run it, read the message, fix it. This counts for the Bug Hunter badge.
Slide bug: the missing hypotenuse
This code comes from a course slide. It has a small bug. Find it and fix it.
- Variable
- A box with a name for a value.
- Type
- The kind of a value: string, number, boolean, undefined, null or object.
- typeof
- An operator that gives the type as text.
- Operator
- A sign that works with values, like
+,%or+=.
In short
letfor values that change,constfor values that stay.typeofnames the type of a value.%gives the remainder,+=changes a value,+joins text.Mathrounds and calculates;Math.random()makes random numbers.
Text tools and arrays: many values in one list.
Sources
- MDN Web Docs (2026). Storing the information you need: Variables.
- MDN Web Docs (2026). JavaScript data types and data structures and typeof.
- MDN Web Docs (2026). Basic math in JavaScript: numbers and operators and Remainder (%).
- MDN Web Docs (2026). Math.
End of the lesson
This lesson: 0 of 105 XP
Code Cards in this lesson: 14
letconsttypeofstringnumberbooleanundefinedobject+ - * /+++="a" + "b"Math.round()Math.random()