Skip to content

Data Types

Understand strings, numbers, booleans, and more — the fundamental kinds of data every program works with.

What you'll learn

  • What a data type is and why it matters
  • The core primitive types: string, number, boolean
  • How null and undefined represent 'no value'
  • The difference between primitive and composite types

Concept

Data Types

A data type is a category of data. It tells the computer what kind of value is stored and what operations make sense. You can multiply two numbers, but you cannot multiply "apple" by "orange" — the types determine what is valid.

The Core Primitives

JavaScript has a handful of primitive types — single, indivisible values:

| Type | Example | Meaning | |------|---------|---------| | String | "hello" | Text, wrapped in quotes | | Number | 42, 3.14 | Integers and decimals | | Boolean | true, false | Exactly two values | | null | null | Intentional "no value" | | undefined | undefined | A variable with no value yet |

Strings

A string is text. Use single or double quotes, or backticks for template literals that allow interpolation:

const name = "Ada";
const greeting = `Hello, ${name}!`;   // "Hello, Ada!"

Booleans

A boolean is a true/false flag. They are the output of comparisons (5 > 3 is true) and the input of if statements. Booleans are how programs make decisions.

Primitives vs. Composites

Primitives (string, number, boolean) hold a single value. Composite types — arrays and objects — hold many values together. You will meet them in later lessons. The distinction matters: primitives are copied by value, composites by reference, which affects how they behave when you assign them.

Example

              const text = "Hello";          // string
const count = 42;            // number
const isActive = true;       // boolean
const empty = null;          // intentional empty
let notAssigned;             // undefined

// The typeof operator reveals a value's type:
console.log(typeof text);        // "string"
console.log(typeof count);       // "number"
console.log(typeof isActive);    // "boolean"
console.log(typeof empty);       // "object" (a famous JS quirk!)
console.log(typeof notAssigned); // "undefined"

// Operations depend on type:
console.log(10 + 5);        // 15  (number + number)
console.log("10" + "5");    // "105" (string + string = concatenation!)
            

Notice how + behaves differently for numbers vs. strings. This is why knowing the type matters — the same operator can do different things.

Try it

  • JSON Formatter

    JSON is built from strings, numbers, booleans, null, arrays, and objects. Paste data and identify each type.

Common mistakes

The mistake

Adding a number to a string and expecting numeric addition.

The fix

Use Number() or parseInt() to convert the string first: Number('5') + 5 gives 10, not '55'.

The mistake

Confusing null with undefined.

The fix

Use null when you intentionally set 'no value'. undefined means a variable was never assigned. They are different.

Related Snippets

Practice Data Types

2 exercises

Practice Data Types

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors