typeof: What's in the Box?

Grade 2, page 9 of 13. A one-minute read.

Sometimes you want to know what type of value a variable is holding. JavaScript has a handy operator for that called typeof. It gives you back the type's name as a string:

JavaScript
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof 3.14);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof "42");

It prints

string
number
number
boolean
undefined
string

Notice the last one: "42" in quotes is a string, not a number. typeof doesn't care what the characters look like, only what type the value actually is.

Watch outThe most famous bug in JavaScript

Try this:

JavaScript
console.log(typeof null);

It prints

object

What?! null is not an object! This is a genuine mistake from the very first version of JavaScript back in 1995. By the time people realized, so many websites depended on it that fixing it would have broken half the internet. So it's stayed that way ever since. Just remember: typeof null says "object", and that's a bug that will never be fixed. Buggsy is extremely proud of this one.

Professor’s CornerBigInt and Symbol

Regular JavaScript numbers can only store whole numbers exactly up to 9,007,199,254,740,991 (which you can see by typing Number.MAX_SAFE_INTEGER). Past that, they start to lose precision. For truly enormous whole numbers, JavaScript has BigInt: just add an n to the end, like 12345678901234567890n. You'll rarely need it, unless you're counting grains of sand or the national debt.

Symbols are special unique values used mostly by advanced programmers and library authors to create identifiers that can never clash with each other. You can safely ignore them for a long, long time.

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning