Courses Learn Blog Contact
Get Started →
Lesson 3

Data Types

JavaScript Simplified · One concept, one day, one project · Day 3 of 32 Data Types The different kinds of value JavaScript can hold, and why the kind matters. You store a product’s details using the right type for each o

JavaScript Simplified 4 min read Free

JavaScript Simplified · One concept, one day, one project · Day 3 of 32

Data Types

The different kinds of value JavaScript can hold, and why the kind matters. You store a product’s details using the right type for each one.

By the end of today you can

  • Name the basic data types: string, number, boolean, undefined and null
  • Choose the right type for a piece of information
  • Check a value’s type with typeof
  • Explain the difference between undefined and null in plain words

Yesterday you stored a name (text) and an age (a number) without thinking about the difference. JavaScript does think about it. Every value has a type, and the type decides what you can do with the value.

1. Why Types Matter

Definition

A data type is the kind of value something is: text, a number, a yes/no, and so on. The type matters because JavaScript treats each kind differently. Adding two numbers gives a sum; "adding" two pieces of text joins them.

What this code doesThe same + does two different jobs. With numbers it adds (15). With text it joins ("105"). The type decides.
app.js
1
2
3
4
// Numbers add up.
console.log(10 + 5);
// Text joins together (this is called concatenation).
console.log("10" + "5");
OUTPUT   TERMINAL
➜ webbo3 node app.js
15
105

2. The Basic Types

Type 1: String (text)

A string is text, written inside quotes. Single or double quotes both work, as long as they match.

What this code doesTwo strings. The quotes are how JavaScript knows it is text and not a variable name.
app.js
1
2
3
4
const firstName = "Ada";
const greeting = "Welcome back";
console.log(firstName);
console.log(greeting);
OUTPUT   TERMINAL
➜ webbo3 node app.js
Ada
Welcome back

Type 2: Number

A number is any number, written without quotes. Whole numbers and decimals are both just numbers in JavaScript.

What this code doesNo quotes, so these are numbers. age + 1 really adds, giving 23.
app.js
1
2
3
4
const age = 22;
const price = 1500.50;
console.log(age + 1);
console.log(price);
OUTPUT   TERMINAL
➜ webbo3 node app.js
23
1500.5

Type 3: Boolean (true or false)

A boolean is one of exactly two values: true or false. It answers a yes/no question, and it is the heart of every decision a program makes.

What this code doesTwo booleans. No quotes: true and false are special values, not text.
app.js
1
2
3
4
const isLoggedIn = true;
const hasPaid = false;
console.log(isLoggedIn);
console.log(hasPaid);
OUTPUT   TERMINAL
➜ webbo3 node app.js
true
false

Type 4: Undefined and Null

undefined means "this variable has no value yet". JavaScript sets it automatically when you make a box but put nothing in it. null means "deliberately empty": you chose to put nothing here.

What this code doesnotSetYet is undefined because nothing was stored. middleName is null because we chose to leave it empty on purpose.
app.js
1
2
3
4
let notSetYet;            // made, but nothing put in: undefined
let middleName = null;    // deliberately empty
console.log(notSetYet);
console.log(middleName);
OUTPUT   TERMINAL
➜ webbo3 node app.js
undefined
null

3. Checking a Type

When you are not sure what type a value is, typeof tells you. It is useful when a value came from somewhere else and you want to be certain.

What this code doestypeof reports the type of whatever follows it.
app.js
1
2
3
4
console.log(typeof "Ada");     // a string
console.log(typeof 22);        // a number
console.log(typeof true);      // a boolean
console.log(typeof undefined); // undefined
OUTPUT   TERMINAL
➜ webbo3 node app.js
string
number
boolean
undefined
TypeWhat it holdsExample
StringText, in quotes"Ada"
NumberAny number22
Booleantrue or falsetrue
UndefinedNo value yetlet x;
NullDeliberately emptylet x = null;

Today’s Mini Project

📦 Product Information Card

Store a product’s details using the right type for each: a name (string), a price (number), whether it is in stock (boolean). Then print a card.

What this code doesEach detail uses the type that fits it. The price is a number, so it could be used in a calculation later; the name is a string; in-stock is a boolean.
app.js
1
2
3
4
5
6
7
8
9
const productName = "Wireless Mouse";   // string
const price = 7500;                      // number
const inStock = true;                    // boolean
const discount = null;                   // no discount set

console.log("Product: " + productName);
console.log("Price: N" + price);
console.log("In stock: " + inStock);
console.log("Discount: " + discount);
OUTPUT   TERMINAL
➜ webbo3 node app.js
Product: Wireless Mouse
Price: N7500
In stock: true
Discount: null
Challenge (15 minutes)

Add a number variable quantity set to 3, then print a line that shows the total: "Total: N" + (price * quantity). Expected: a Total line showing 22500.

Try This

Make a variable holding your age as a number, and another holding your age in quotes as text. Print typeof each one.

Expected: "number" then "string". Same digits, different types, different behaviour.

Picture This

Two values can look the same on screen, like 22 and "22", yet behave completely differently: one adds, one joins. Seeing the type behind a value is a habit that saves you from a whole family of confusing bugs.

Checkpoint

Why does "10" + "5" give "105" and not 15?

Because both values are strings (text), and + joins text. To add them as numbers, they must be numbers (10 + 5), without quotes.

What is the difference between undefined and null?

undefined means a variable has no value yet, usually set by JavaScript automatically. null means deliberately empty: you chose to put nothing there.

How do you check what type a value is?

With typeof, for example typeof 22 reports "number". It is handy when you are unsure what kind of value you are holding.

Tick Before You Move On

  • ☐ I can name the basic types: string, number, boolean, undefined, null
  • ☐ I can pick the right type for a piece of information
  • ☐ I can use typeof to check a value
  • ☐ I built the Product Information Card

Quick Recap

Every value has a type, and the type decides what you can do with it. The basics: string (text), number, boolean (true/false), undefined, null. undefined is "no value yet"; null is "deliberately empty". typeof tells you the type of any value.

Next: Day 4. Now that you have numbers and text, you will do things with them: add, compare, and combine, using operators.

Learn this with a real instructor

Free lessons take you a long way on your own. The full programmes add live classes, assignments, Skill Points and a certificate — built for African students.