25 min·beginner

Lesson 4

Primitive data types

Build a practical mental model for primitive data types and use it in working JavaScript.

By the end, you will be able to…

  • Explain primitive data types in your own words
  • Use primitive data types in a small program
  • Recognize and debug a common failure mode

Build the mental model

JavaScript has seven primitive types: string, number, boolean, undefined, null, symbol, and bigint. Primitives are compared and copied by value, which is the foundation every later lesson on objects and references builds on.

Syntax and working example

Read the code from inputs to output. Predict what it prints, then run it and change one value.

Working example
Expected output: "string" "number" "boolean" "undefined" "object"

Why this matters

Knowing exactly which type a value is prevents entire categories of bugs, especially around comparisons and math on unexpected types.

Real-world example

Form input validation constantly checks typeof or Number.isFinite before trusting user-entered data.

Common mistakes

  • Trusting typeof null to return "null" (it returns "object")
  • Confusing undefined (never assigned) with null (intentionally empty)
  • Assuming a number typed into a form field arrives as a number, not a string

Try it yourself

Challenge

Log the typeof for five different values, including null and an array.

Practice

Knowledge check

What does typeof null return, famously surprising newcomers?

Summary

Primitives are copied by value and have exactly one of seven types — know them to reason about comparisons correctly.