JS Sketchbooksee JavaScript think ✏️
← back to phase 1

1.8 — typeof & dynamic typing

You’ve met the types. Now two questions with one answer: how do you ask the machine what type something is — and does a variable itself have a type at all?

The tool is typeof, and the answer to the second question is the deep one: values have types — variables are just labels. The same label can point at a number today and a string tomorrow. That freedom has a name, dynamic typing, and it’s equal parts superpower and trap.

watch it happen
let mystery = 42;
console.log(typeof mystery);
mystery = "forty-two";
console.log(typeof mystery);
console.log(typeof true);

A box with 42 inside. Look closely at the picture: the type tag — “number” — hangs on the VALUE in the box, not on the label. Keep your eye on that tag through this lesson.

mystery42number 🏷️
under the hood

The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”

The proper vocabulary: JavaScript is dynamically typed — types are checked while the program runs, and any variable may hold any type.

The opposite is statically typed — Java, C#, and JavaScript’s cousin TypeScript. There, a variable declares its type. The machine refuses mismatches before the program ever runs. A taste of TypeScript comes in Phase 8 — most Playwright projects use it to catch type mix-ups early.

Because typeof answers with a string, the idiom you’ll see everywhere is a string comparison: typeof x === "number". Mind the quotes on the right side — comparing against bare number would be a ReferenceError (no such variable!).

A delightful brain-stretcher: typeof typeof 42 is… "string" — because typeof 42 produces the string "number", and the type of a string is "string". Evaluate inside-out, like always.

💼 On the job — why should a tester care about dynamic typing? Because it is a bug factory. Nothing stops a variable that held a number from silently becoming a string — you saw how easily. Suddenly total + 1 glues instead of adds. Entire categories of production bugs boil down to “this value was not the type everyone assumed.” Many of your future test cases target exactly that. The next lesson shows what happens when mixed types collide.

your turn

✏️ Quick check 1

Type what typeof "42" gives (as the console would show it):

✏️ Quick check 2

Type exactly what prints:

let x = 5;
x = "five";
console.log(typeof x);

✏️ Quick check 3

The tiny puzzle: type what typeof typeof true gives.

teach it back

🗣️ Now teach it back

Explain to a friend: what does “dynamic typing” mean, where does the type actually live (label or value?), and what does typeof really check?

Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.

a few sentences, minimum — you’ve got this
to remember
Types live on VALUES; variables are typeless labels. Dynamic typing = any label may point at any type, any time.
typeof reads the current value’s tag and always answers with a string: typeof x === "number".
Cheat sheet: "number", "string", "boolean", "undefined" — and typeof null → "object" (the permanent bug). typeof never throws.