Lesson 9 +10 XP

JavaScript Data Types

JavaScript Data Types

A data type describes what kind of value something is. JavaScript is loosely typed, so you do not declare types, the values decide.

The primitive types

  • String: text like "Hello"
  • Number: 5, 3.14, -10
  • Boolean: true or false
  • Undefined: a variable with no value
  • Null: an intentional empty value
  • BigInt: very large integers like 123n
  • Symbol: unique identifiers

Reference types

  • Object: collections of key-value pairs
  • Array: ordered lists of values
  • Function: reusable blocks of code

Declaring different types

let name = "Ada";          // string
let age = 36;              // number
let isCool = true;         // boolean
let person = { name: "Ada" }; // object
let scores = [10, 20, 30]; // array

Dynamic typing

A variable can hold different types at different times:

let value = "text"; // string
value = 42;         // now a number

Checking a type

Use typeof to see a value's type:

typeof "hello"; // "string"
typeof 42;      // "number"

TL;DR

  • Primitive types: string, number, boolean, undefined, null, bigint, symbol.
  • Reference types: objects, arrays, functions.
  • JavaScript is loosely typed; the value decides the type.
  • typeof reveals a value's type.