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.
+ does two different jobs. With numbers it adds (15). With text it joins ("105"). The type decides.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.
Type 2: Number
A number is any number, written without quotes. Whole numbers and decimals are both just numbers in JavaScript.
age + 1 really adds, giving 23.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.
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.
notSetYet is undefined because nothing was stored. middleName is null because we chose to leave it empty on purpose.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.
typeof reports the type of whatever follows it.| Type | What it holds | Example |
|---|---|---|
| String | Text, in quotes | "Ada" |
| Number | Any number | 22 |
| Boolean | true or false | true |
| Undefined | No value yet | let x; |
| Null | Deliberately empty | let 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.
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.
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.
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.