Primitive data types
Storing someone's age isn't the same as storing their name, or whether an order is paid. A number, a piece of text and a yes/no behave differently, and JavaScript knows it: every value has a type. These are the basic ones:
| Type | Example | What it's for |
|---|---|---|
number |
42, 3.14 |
Integers and decimals |
string |
"hello" |
Text |
boolean |
true, false |
True or false |
undefined |
undefined |
Value not assigned yet |
null |
null |
Intentional absence of a value |
bigint |
9007199254740993n |
Very large integers |
symbol |
Symbol("id") |
Unique identifiers |
The typeof operator
It's used to find out the type of a value:
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
Fun fact:
typeof nullreturns"object". It's a historical bug in the language that is kept for compatibility.
undefined vs null
undefined: the language hasn't given a value to something yet.null: you, as the programmer, indicate "there is no value here".
Examples
Inspecting types with typeof
let age = 30;
let name = "Ana";
let active = true;
console.log(typeof age, typeof name, typeof active);