JS enginexynumberstringnullbooleanbigintundefinedsymbolobject1let x = 42;2console.log(typeof x);3x = "42";4console.log(typeof x);5console.log(x + 1);6console.log(Number(x) + 1);7x = true;8console.log(typeof x);9let y;10console.log(typeof y);Consolenumbertypes.js:2stringtypes.js:4421types.js:543types.js:6booleantypes.js:8undefinedtypes.js:104242"42""42""421"43trueundefined"number""string""boolean""undefined"
Back: the JavaScript engine, the boxes x and y (value and type), and the shelf of the eight typesFront: the script types.js and the developer consoleTokens: instructions, values (42, "42", true, undefined) and the type names typeof givesAt the back, the JavaScript engine and two memory boxes, x and y, and a shelf of the eight types. In front, the script types.js and the developer console. x holds 42, then "42", then true: the box stays the same while the value, and the value's type, change. typeof reads the type of the value inside. Types change what + does: "42" + 1 joins text into "421"; Number(x) + 1 adds to 43. typeof null gives "object", not "null". It is a mistake from the first version of JavaScript, kept so old web pages still work. Check for null with x === null.

Values have types

  1. A program works with values: pieces of data such as 42, "hi" or true. Every value has a type. The type says what kind of data it is and what you can do with it. JavaScript has eight types.

One variable, changing types

Types change what + does

More types

What happens

// step 1/10 · values
value   a piece of data:  42   "hi"   true
type    what kind of data a value is
        number  string  boolean  undefined
        null  bigint  symbol  object
JavaScript has exactly these 8 (ECMA-262 §6.1)

The console shows a string without its quotes, so "421" appears as 421. Many consoles colour numbers differently from strings so you can still tell them apart. typeof null gives "object", not "null". It is a mistake from the first version of JavaScript, kept so old web pages still work. Check for null with x === null.