DevPath · Learn to code ESPTEN

Variables and data types

Primitive data types

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 null returns "object". It's a historical bug in the language that is kept for compatibility.

undefined vs null

Examples

Inspecting types with typeof

let age = 30;
let name = "Ana";
let active = true;
console.log(typeof age, typeof name, typeof active);
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← Declaring variables: let and constView the module →