Lesson 3: JavaScript Data Types

Learning Objectives

By the end of this lesson, learners will:

  • Understand what data types are and why they matter in programming
  • Learn about JavaScript’s primitive and non-primitive data types
  • Be able to declare and use different data types
  • Recognize the difference between undefined and null
  • Use the typeof operator to check data types

What Are Data Types?

Every piece of data in a JavaScript program has a type.
A data type tells JavaScript:

  • What kind of value it is
  • What you can do with it

📦 Think of it like different containers:

  • A bottle for water (number)
  • A folder for documents (string)
  • A switch for lights (boolean)

Categories of Data Types

JavaScript has two major categories of data types:

1. Primitive Data Types

These are the most basic values in JavaScript. They are immutable (cannot be changed).

TypeExampleDescription
String"hello"Text values
Number42, 3.14Whole and decimal numbers
Booleantrue, falseLogical values
Undefinedlet x;Declared but no value assigned
Nulllet x = null;Intentional empty value
BigInt12345678901234567890nVery large integers
SymbolSymbol("id")Unique identifiers (advanced)

2. Non-Primitive Data Types (Objects)

These store collections of data or more complex entities.

TypeExampleDescription
Object{ name: "Alice", age: 25 }Key-value pairs
Array[1, 2, 3]Ordered list of items
Functionfunction greet() {}Reusable blocks of code

We’ll explore objects, arrays, and functions in later modules.


Primitive Data Types Explained

1. Strings

  • Represent text and are wrapped in quotes.
  • Can use single ('), double ("), or template literals (`).

📌 Example:

let name = "Alice";
let greeting = `Hello, ${name}!`; // using template literals

2. Numbers

  • Can be whole numbers or decimals.
  • JavaScript uses only one number type (no distinction between integers and floats).

📌 Example:

let age = 30;
let price = 19.99;

3. Booleans

  • Only two possible values: true or false.
  • Used for decision-making (conditions, comparisons).

📌 Example:

let isStudent = true;
let passed = false;

4. Undefined

  • A variable that’s declared but not assigned a value.

📌 Example:

let x;
console.log(x); // undefined

5. Null

  • A variable with a deliberate empty value.
  • Used to “clear out” or “reset” a variable.

📌 Example:

let user = null;

🔍 undefined means “not assigned.”
🔍 null means “intentionally empty.”


6. BigInt (Advanced Use)

  • Used to store numbers larger than JavaScript’s safe limit.
  • Add n to the end of a number.

📌 Example:

let big = 123456789012345678901234567890n;

7. Symbol (Advanced Use)

  • A unique, immutable value used mostly in advanced JavaScript features like object keys.

📌 Example:

let id = Symbol("id");

Using the typeof Operator

Use the typeof keyword to check a variable’s type:

📌 Example:

let name = "Alice";
console.log(typeof name); // "string"

let age = 30;
console.log(typeof age); // "number"

let isOnline = true;
console.log(typeof isOnline); // "boolean"

Practice Examples

let city = "Berlin";        // String
let temperature = 22.5;     // Number
let isSunny = true;         // Boolean
let nothingHere = null;     // Null
let notSet;                 // Undefined
let largeNumber = 9007199254740991n; // BigInt

Summary Table

TypeKeywordExampletypeof result
String"...""Hello""string"
NumberAny num42, 3.14"number"
Booleantrue/falsetrue"boolean"
Undefinedlet x;"undefined"
Nullnulllet x = null;"object" (quirk)
BigIntn123n"bigint"
SymbolSymbol()Symbol("id")"symbol"