Part of the “Web Basics for Young Coders” course — written for curious 10‑ to 13‑year‑olds.
In Module Three, you learned that variables are like labelled boxes. You learned how to create them, name them,
and store different kinds of data — numbers, strings, booleans, undefined, and null. You also learned how to
change values, combine strings, and check data types with typeof.
Now it is time to go deeper. In Module Four, we will explore special ways to work with data. You will learn how to change data types on purpose, how to work with more complex data like arrays and objects, how to use special values carefully, and how to avoid common traps that confuse beginners.
Think of Module Three as learning to ride a bicycle with training wheels. Module Four is where we take the training wheels off and learn to turn, brake, and go faster — safely. You will still use everything you learned before, but now you will become a stronger, smarter coder.
This module has 13 lessons. Each lesson is short and clear. Take your time. Try the examples. Ask questions. By the end, you will be able to build small web programs that store and change real information — just like real websites do.
Let’s begin!
After finishing this module, you will be able to:
Number(), String(), and Boolean().undefined, null, and NaN.typeof and simple checks to verify data.Chidi lives in Enugu, Nigeria. Every Saturday, his mother sends him to the market with a list. One Saturday, the list said: “Buy 3 yams, 2 tomatoes, 1 tin of milk, and check if the price of rice is below ₦1000 per cup.”
Chidi looked at the list and thought, “This is not just one thing. This is a list of things. And each thing has a name and a number.” He decided to organise it in his notebook like this:
Item | Quantity
------------|---------
Yam | 3
Tomato | 2
Milk | 1
Rice price | below 1000? (true/false)
When he got to the market, he checked the rice price. It was ₦950 per cup. So he wrote “true” next to “Rice price below ₦1000?”. He bought everything and went home.
In web programming, Chidi’s notebook is like an array (a list of items) and an object (a collection of related information). In this module, you will learn how to build these powerful tools. You will also learn how to convert data when needed — like changing a price written as text into a real number so you can compare it.
Are you ready to become a data master? Let’s go!
A data type is the kind of value a variable holds — number, string, boolean, and so on.
JavaScript treats each type differently. If you mix them up, you can get strange results that are hard to find.
Imagine you have a box of eggs and a box of stones. If you try to bounce them, the eggs break. Different things behave differently. In JavaScript, numbers add, strings join, and booleans help make choices.
If a shop’s price is stored as text ("500") instead of a number (500),
adding two prices might give "500300" instead of 800.
If your test scores are strings, averaging them will not work correctly.
If your shopping list total is a string, you cannot calculate change properly.
If bus fares are strings, adding them gives "500300" instead of 800 naira.
let a = 5; // number
let b = "5"; // string
console.log(a + a); // 10
console.log(b + b); // "55"
Always use the correct data type for the job. Numbers for math, strings for text, booleans for yes/no.
Primitive types are simple, single values (number, string, boolean, undefined, null, symbol). Objects are collections that can hold many values.
Primitives are copied by value. Objects are copied by reference. This changes how they behave when you change them.
A primitive is like a single coin. An object is like a wallet that holds many coins and cards.
Your age is a primitive (one number). Your school bag is an object (holds books, pens, snacks).
A single test score is a primitive. A report card is an object (holds scores for many subjects).
The number of eggs is a primitive. The fridge is an object (holds eggs, milk, vegetables).
One naira coin is a primitive. A purse full of naira notes and coins is an object.
Primitive: let age = 12;
Object: let student = { name: "Ada", age: 12, class: "JSS2" };
Primitives are single values. Objects hold many values together. We will learn more about objects later in this module.
Type conversion means changing a value from one type to another.
User input from forms always arrives as strings. To do math, you must convert to numbers.
Use Number("123") to turn text into a number. Use String(123) to turn a number into text.
When you type your age in a form, it arrives as "12". Convert it to 12 to add 1 for next year.
If your score is given as "85", convert it to 85 to find the class average.
If your recipe says "3" cups of rice, convert to 3 to double the recipe.
If a trader says the price is "₦500" (string), convert the numeric part to 500 to calculate change.
let strNum = "10";
let num = Number(strNum); // 10
let backToStr = String(num); // "10"
console.log(typeof num); // "number"
console.log(typeof backToStr); // "string"
Use Number() and String() to switch between types when needed.
Boolean() converts a value to true or false. Some values are
“falsy” (become false) and others are “truthy” (become true).
This helps you check if something exists, is empty, or is zero.
Falsy values include: false, 0, "" (empty string),
null, undefined, and NaN. Everything else is truthy.
An empty plate is falsy (no food). A plate with food is truthy.
An empty notebook (0 pages written) is falsy. A notebook with notes is truthy.
A phone with 0% battery is falsy. A phone with 50% battery is truthy.
An empty fuel tank (0 litres) is falsy. A tank with fuel is truthy.
Boolean(0); // false
Boolean(""); // false
Boolean("hello"); // true
Boolean(42); // true
Boolean(null); // false
Falsy values: false, 0, "", null, undefined, NaN. Everything else is truthy.
NaN means “Not a Number”. It appears when you try to do math with something that is not a number.
NaN can silently break your calculations. You must check for it.
If you try to multiply "hello" * 2, you get NaN. It is JavaScript’s way of saying
“I cannot do this math.”
If you try to add your name to a number, you get a strange result — that is like NaN.
If a test score is missing and you try to average it, you might get NaN.
If a recipe says “add some salt” (no number) and you try to double it, you get NaN.
If a price tag is torn and you try to add it, you get NaN — not a number.
let result = "hello" * 2;
console.log(result); // NaN
console.log(typeof result); // "number" (NaN is a number type!)
NaN means “Not a Number”. It appears when math fails. Use isNaN() to check for it.
An array is a list of values stored in one variable. You use square brackets [ ].
Arrays let you store many items — like a shopping list or a list of scores — without making many variables.
Instead of let item1 = "Yam"; let item2 = "Tomato";, you write
let items = ["Yam", "Tomato", "Milk"];.
Your playlist is an array of songs. Your contacts list is an array of names.
Your list of subject scores is an array: [85, 90, 78, 92].
Your shopping list is an array: ["Rice", "Beans", "Oil"].
The Super Eagles starting eleven is an array of player names:
["Okoye", "Omeruo", "Ajayi", ...].
let fruits = ["Mango", "Orange", "Banana"];
console.log(fruits[0]); // "Mango"
console.log(fruits.length); // 3
Arrays store lists of values. Use square brackets. Access items with their index starting at 0.
Arrays have built-in tools (methods) to add, remove, and count items.
Lists change all the time. You need to add new items and remove old ones.
push() adds an item to the end.pop() removes the last item.length tells you how many items there are.Adding a new contact to your phone (push). Deleting the last message (pop).
Adding a new score to your list (push). Removing the last score (pop).
Adding “Eggs” to your shopping list (push). Removing the last item you already bought (pop).
Adding a new player to the Super Eagles squad (push). Removing a player who is injured (pop).
let items = ["Yam", "Tomato"];
items.push("Milk"); // ["Yam", "Tomato", "Milk"]
items.pop(); // ["Yam", "Tomato"]
console.log(items.length); // 2
Use push() to add, pop() to remove, and .length to count items in an array.
An object is a collection of related information stored as key–value pairs. You use curly braces { }.
Objects let you group related data together — like a student’s name, age, and class.
Think of a student ID card. It has a name, an age, a class, and a photo. All those pieces belong together. That is an object.
Your phone contact is an object: { name: "Ada", phone: "08012345678", email: "ada@mail.com" }.
A student record: { name: "Chidi", age: 13, class: "JSS3", scores: [85, 90] }.
A recipe: { name: "Jollof Rice", servings: 4, time: "45 minutes" }.
A state record: { name: "Lagos", capital: "Ikeja", population: 15000000 }.
let student = {
name: "Ada",
age: 12,
class: "JSS2",
likesCoding: true
};
console.log(student.name); // "Ada"
Objects store related data as key–value pairs. Use dot notation (student.name) to read values.
You can put arrays inside objects and objects inside arrays. This creates rich data structures.
Real-world data is often nested. A class has many students. Each student has many scores.
Think of a school as an object. Inside it, there is an array of students. Each student is an object with their own scores (another array).
A music app: an artist object with an array of albums. Each album has an array of songs.
A class object with an array of student objects. Each student has an array of subject scores.
A fridge object with an array of shelf arrays, each holding items.
A football team object with an array of player objects, each with their own stats.
let school = {
name: "Bright Future Academy",
students: [
{ name: "Ada", age: 12, scores: [85, 90] },
{ name: "Chidi", age: 13, scores: [78, 88] }
]
};
console.log(school.students[0].name); // "Ada"
console.log(school.students[0].scores[1]); // 90
You can nest arrays and objects. Use dot and bracket notation to reach inside.
undefined: a variable has no value yet.null: an empty value given on purpose.NaN: “Not a Number” — a failed math result.They look similar but mean different things. Confusing them causes bugs.
undefined is like an empty box you forgot to fill. null is like a box you
deliberately left empty. NaN is like a broken calculator showing an error.
Your friend’s phone number is undefined if you never saved it. Your lunch box is null if you decided to fast. Your maths answer is NaN if you tried to add “hello” to 5.
A test score is undefined if you were absent. A homework mark is null if the teacher chose not to grade it. A calculated average is NaN if one score was text instead of a number.
The TV remote battery level is undefined if the remote is broken. Your pocket money is null if you spent it all. The total bill is NaN if you typed “five” instead of 5.
The price of petrol at a closed station is undefined. The number of passengers in an empty bus is null. The total fare is NaN if you added “₦500” to 300 without converting.
let a; // undefined
let b = null; // null
let c = "hi" * 2; // NaN
console.log(typeof a); // "undefined"
console.log(typeof b); // "object" (a famous quirk)
console.log(typeof c); // "number"
undefined = no value yet. null = empty on purpose. NaN = failed math. Learn to tell them apart.
You can check data types using typeof, Array.isArray(), and
isNaN().
Before you use a value, you should know what type it is. This prevents errors.
typeof x tells you the primitive type.Array.isArray(x) tells you if x is an array.isNaN(x) tells you if x is “Not a Number”.Before cooking, you check if the ingredient is salt or sugar. Before using a value, check its type.
Before grading, check if the answer is a number or a word.
Before using a remote, check if it is for the TV or the fan.
Before accepting a naira note, check if it is ₦1000 or ₦500.
let age = 12;
let fruits = ["Mango", "Orange"];
let broken = "hello" * 2;
console.log(typeof age); // "number"
console.log(Array.isArray(fruits)); // true
console.log(isNaN(broken)); // true
Use typeof for primitives, Array.isArray() for arrays, and
isNaN() to check for NaN.
Good variable names describe what the variable holds. They make code easy to read.
You will read your code many times. Clear names save you from confusion.
Use camelCase: playerScore, favouriteFood, isLoggedIn.
Label kitchen jars “Rice”, “Beans”, “Sugar” — not “Jar1”, “Jar2”.
Name your notebooks “Maths”, “English”, “Science” — not “Book1”, “Book2”.
Name phone contacts “Mum”, “Dad”, “Ada” — not “Contact1”, “Contact2”.
Name your variables jollofRicePortions not jrp. Clear names help everyone.
✅ let playerScore = 100;
✅ let isGameOver = false;
✅ let favouriteNigerianFood = "Jollof Rice";
❌ let x = 100;
❌ let y = false;
Use clear, descriptive names. camelCase for multi-word names. Avoid single letters except in loops.
Now we combine arrays, objects, and type conversion to build a small, useful program.
This is how real web apps store and use data — lists of users, products, scores, and more.
Let’s create a student object with an array of scores, then calculate the average.
A school app that stores student records and calculates their average score.
A class register that shows “Ada scored 85, 90, 78. Average: 84.33”.
A shopping list app that stores items and their prices, then calculates the total.
A football app that stores player stats and calculates goals per match.
let student = {
name: "Ada",
scores: [85, 90, 78]
};
let total = student.scores[0] + student.scores[1] + student.scores[2];
let average = total / student.scores.length;
let message = `${student.name} scored an average of ${average}.`;
console.log(message);
// Output: Ada scored an average of 84.33333333333333.
Objects + arrays + numbers = powerful data handling. You just built a real data structure!
| Word | Simple Definition |
|---|---|
| Primitive | A simple, single value like a number or string. |
| Object | A collection of related values stored as key–value pairs. |
| Array | A list of values stored in one variable. |
| Index | The position of an item in an array, starting at 0. |
| Element | An item inside an array. |
| Property | A key–value pair inside an object. |
| Method | A built-in action for a data type, like push(). |
| Type Conversion | Changing a value from one type to another. |
| Truthy | A value that becomes true when converted to boolean. |
| Falsy | A value that becomes false when converted to boolean. |
| NaN | “Not a Number” — a failed math result. |
typeof | An operator that tells you the data type. |
Array.isArray() | A method that checks if a value is an array. |
isNaN() | A function that checks if a value is NaN. |
Number(), String(), and Boolean().[ ], index starting at 0, and .length.{ } and dot notation.typeof, Array.isArray(), and isNaN() to check types safely.let items = [ ]; to create an empty array.items.push("Yam");.items[0].items.length.items.pop();.let student = { name: "Ada", age: 12 };.student.name.student.class = "JSS2";.student.age = 13;."name" in student.let input = "42";.let num = Number(input);.if (isNaN(num)) { console.log("Not a number!"); }.num in math: num + 8; // 50.let marketList = ["Yam", "Tomato", "Milk"]; (array).{ name: "Jollof Rice", servings: 4, spicy: true } (object).let trafficLevels = ["light", "heavy", "go-slow"]; (array).{ team: "Super Eagles", players: ["Okoye", "Omeruo"], goals: 2 }.let movies = [{ title: "The Wedding Party", year: 2016 }, { title: "Living in Bondage", year: 1992 }];.let inventory = ["Sword", "Shield", "Potion"];let team = [{ name: "Spider-Boy", power: 8 }, { name: "Fire Girl", power: 9 }];let toppings = ["Cheese", "Pepperoni", "Mushroom"];let wishlist = ["Bicycle", "Book", "Game"];let colours = ["Blue", "Green", "Red"];typeof null returns "object" — a famous bug from 1995 that was never fixed.NaN is the only value in JavaScript that is not equal to itself: NaN === NaN is false.[42, "hello", true, null].[[1, 2], [3, 4]] — called a 2D array.{ a: { b: { c: 1 } } } — called nested objects.length property of an array is always one more than the highest index.Number(), String(), and Boolean().[ ], index starts at 0, .length gives count.{ }, access with dot notation.typeof, Array.isArray(), and isNaN().| Mistake | Why it’s wrong | Fix |
|---|---|---|
let x = "10"; let y = x + 5; | Results in "105" (string) not 15. | let y = Number(x) + 5; |
let arr = [1, 2, 3]; console.log(arr[3]); | Index 3 does not exist — arrays start at 0. | Use arr[2] for the third item. |
let obj = { name: "Ada" }; console.log(obj[name]); | Missing quotes around key. | obj["name"] or obj.name |
let x = null; console.log(x + 1); | null converts to 0, so result is 1 — may surprise you. | Check for null before math. |
let x = "hello" * 2; | Result is NaN, not an error. | Use isNaN() to check. |
const by default, and let only when you need to change the value.playerScore not x.myFavouriteFood.;.typeof, Array.isArray(), and isNaN() when debugging.
Index: 0 1 2
+--------+--------+--------+
Array | "Yam" |"Tomato"| "Milk" |
+--------+--------+--------+
| | |
V V V
items[0] items[1] items[2]
+---------------------------+
| student |
+---------------------------+
| name: "Ada" |
| age: 12 |
| class: "JSS2" |
| scores: [85, 90, 78] |
+---------------------------+
Start
|
V
Is it a number? ----> Yes ----> Number
|
No
|
V
Is it in quotes? ----> Yes ----> String
|
No
|
V
Is it true/false? ----> Yes ----> Boolean
|
No
|
V
Is it an array? ----> Yes ----> Array
|
No
|
V
Is it an object? ----> Yes ----> Object
|
No
|
V
It might be undefined, null, or NaN.
1. Create object: let student = {};
2. Add name: student.name = "Ada";
3. Add scores: student.scores = [85, 90, 78];
4. Calculate total: let total = 85 + 90 + 78;
5. Calculate average: let avg = total / 3;
6. Display message: console.log(`${student.name}: ${avg}`);
| Feature | Primitive | Object |
|---|---|---|
| Example | 42, "Ada", true | [1,2,3], { name: "Ada" } |
| Holds | One value | Many values |
| Copied by | Value | Reference |
| Mutable? | No (value is replaced) | Yes (contents can change) |
| Value | Meaning | Example |
|---|---|---|
undefined | No value yet | let x; |
null | Empty on purpose | let y = null; |
NaN | Failed math | "hi" * 2 |
| Feature | Array | Object |
|---|---|---|
| Syntax | [ ] | { } |
| Access | By index (0, 1, 2…) | By key (name, age…) |
| Best for | Lists of similar items | Related information |
| Example | ["Yam", "Tomato"] | { name: "Ada", age: 12 } |
Each lesson above ended with a Mini Summary. That’s your quick recap. If you forget something, go back to that lesson and read the mini summary again.
In this module, you went deeper into variables and data types. You learned why data types matter, how
primitives differ from objects, and how to convert between numbers, strings, and booleans. You discovered
truthy and falsy values, and you met NaN — “Not a Number”. You learned how to create and use arrays to
store lists, and objects to store related information. You even combined arrays and objects to build
rich data structures. You learned to check types safely with typeof, Array.isArray(),
and isNaN(). Finally, you practised good naming and avoided common mistakes. You are now
ready to use these tools in real web projects!
Number("123").let arr = [1, 2, 3];arr[0] for the first item.let obj = { name: "Ada" };obj.name.Array.isArray(value).isNaN(value).Match the item on the left with its correct description on the right.
| Item | Description |
|---|---|
1. [1, 2, 3] | A. Object |
2. { name: "Ada" } | B. Array |
3. "hello" * 2 | C. NaN |
4. let x; | D. Undefined |
5. null | E. Empty on purpose |
Answers: 1-B, 2-A, 3-C, 4-D, 5-E
["Sword", "Shield"]. They pick up a potion. How do you add it?"15". How do you calculate their age next year?[85, 90, 78]. How do you find the average?{ name: "Ada", age: 12 }. How do you add a new property class: "JSS2"?“Data Type Sorting” Game
Work in groups of 3 or 4. Each group gets a set of cards with values on them (e.g., 42,
"Lagos", true, [1,2,3], { name: "Ada" },
null, undefined, NaN). On the board, write five categories:
Number, String, Boolean, Array, Object. One person reads a value. The group must place it in the
correct category. After 10 values, switch roles. The group with the most correct placements wins!
Write down 5 variables you would use to describe your school bag. For each, write the variable name, the value, and the data type. Example:
let bagColour = "Blue"; — Stringlet numberOfBooks = 5; — Numberlet hasLunch = true; — Booleanlet books = ["Maths", "English", "Science"]; — Arraylet owner = { name: "Ada", age: 12 }; — ObjectCreate a simple web page (or a plain text file) that uses an array of student objects to display information about your class. Each student object should have a name, age, and an array of scores. Then calculate the average score for each student and display a message.
Example:
let myClass = [
{ name: "Ada", age: 12, scores: [85, 90, 78] },
{ name: "Chidi", age: 13, scores: [78, 88, 92] },
{ name: "Ngozi", age: 12, scores: [95, 89, 94] }
];
for (let i = 0; i < myClass.length; i++) {
let student = myClass[i];
let total = student.scores[0] + student.scores[1] + student.scores[2];
let average = total / student.scores.length;
console.log(`${student.name} (age ${student.age}) scored an average of ${average}.`);
}
You can write this in a JavaScript file and run it in your browser’s console, or just write it on paper and show your teacher.
fruits with 4 fruit names.push().pop().myPet with properties: name, type,
age, and isFriendly.typeof on each property and print the results.Number(), String(), and Boolean().[ ], index starts at 0, .length gives count.{ }, access with dot notation.typeof, Array.isArray(), and isNaN().undefined, null, and NaN?
In Module Five, we will learn about Operators and Expressions. You will discover how to
do math with variables, compare values, and make decisions using if statements. You will
also learn about logical operators (&&, ||, !) and how
to combine conditions. To get ready, make sure you are comfortable creating variables, using arrays and
objects, and checking data types. Try the practical assignment above. When you are ready, turn the page
and let’s continue your coding adventure!
End of Module Four