Types & Operators
- Data types
- Operators
Wires are installed. Now: what's actually allowed to flow through them?
Every value has a type
JavaScript sorts every value into a type — what kind of thing it is, and what you're allowed to do with it. The three you'll use constantly:
let count = 12; // number
let label = "porch lamp"; // string
let isOn = true; // booleanA number is just that — no quotes, and you can do math with it. A
string is text, and it always needs quotes (single ' or double
", either works, just match the one you opened with) — 12 and "12"
look similar but behave completely differently, as you're about to see.
A boolean is one of exactly two values, true or false — a switch
that's either on or off, nothing in between.
Curious what type a value actually is? typeof count answers that —
try it, it hands back "number" as a string of its own.
Operators: combining and comparing values
+, -, *, and / do arithmetic on numbers, exactly like a
calculator: count + 1, count * 2. But + has a second job — glue two
strings together:
let greeting = "Hi, " + label;That line builds one longer string out of two shorter ones.
To compare two values instead of combining them, JavaScript uses
=== — "is strictly equal to" — and !== for "is not equal to," plus
<, >, <=, >= for ordering numbers. Each of these hands back a
boolean: count === 12 is true, count === 13 is false.
Common mistake
+ doesn't know whether you mean math or gluing until it sees what's on
both sides — and if either side is a string, it glues instead of adding:
"5" + 5 is "55", a six-character string, not 10. Keep your numbers
as actual numbers (no quotes) when you want math, and this trap mostly
disappears.
One more trap, for later: JavaScript also has a looser ==, which
converts types before comparing — it's the reason "5" == 5 claims to
be true. === never does that silent conversion. Use ===, always,
and you'll sidestep a genuinely famous class of JavaScript bugs.
Quick check: types & operators
No score kept — pick again as often as you likeNext: functions — where you'll finally wire up a piece of logic once and trigger it as many times as you like.
There's a thermostat in the lab to read first.