“Variables & Data Types for Web” – Learn how JavaScript remembers information
Welcome back, young web coder! In Module One, you learned what variables and data types are. You learned how to create variables with let and const. You also learned how to check a value’s type with typeof.
Now it is time to go deeper into the three most common data types on the web: strings (text), numbers, and booleans (true/false). These three types appear in almost every website you have ever used. A login page uses strings. A shop page uses numbers. A settings page uses booleans.
In this module, we will learn how to work with each of them, how to use their built-in tools, and how to avoid mistakes. By the end, you will be able to build a simple text-and-number based web calculator and much more.
Let’s begin!
After finishing this module, you will be able to:
length, toUpperCase, and slice.+ operator and template literals.toFixed and parseInt.Math object for rounding and randomness.Chidi is 14 years old and lives in Abuja. He loves football and wants to build a small web page that shows his favourite team’s statistics. The page would show the team name, the number of goals, and whether the team won its last match.
He opened the browser console and created three variables:
let teamName = "Super Eagles"; let goalsScored = 3; let didWin = true;
Chidi explained:
teamName is a string — text inside quotes.goalsScored is a number — a value used for counting.didWin is a boolean — either true or false.Next, he wanted to show the team name in uppercase. He typed:
teamName.toUpperCase(); // "SUPER EAGLES"
He wanted to round a decimal goal average. He typed:
let average = 2.6667; average.toFixed(2); // "2.67"
He wanted to show a random number. He typed:
Math.floor(Math.random() * 10); // random number 0–9
Finally, he used a boolean in a decision:
if (didWin) {
console.log("Well done, " + teamName + "!");
} else {
console.log("Better luck next time.");
}
Chidi smiled. Strings, numbers, and booleans had made his page come alive.
Moral of the story: Strings, numbers, and booleans are the everyday building blocks of web pages. Each has its own tools, and each behaves in its own way.
Definition: A string is a piece of text inside quotes.
Why it is important: Text is everywhere on the web — names, messages, titles, and labels.
Simple explanation: A string is any text wrapped in quotes.
Real-life example: A bank stores customer names as strings.
School example: A teacher stores student names as strings.
Home example: A family stores addresses as strings.
Nigerian example: A trader stores product names as strings.
Illustration:
let name = "Ada"; let city = 'Lagos'; let message = "Welcome!"; typeof name → "string"
Mini summary: Strings are text inside quotes, either single or double.
Definition: Combining strings means joining two or more strings together.
Why it is important: Websites often show messages like "Hello, Ada".
Simple explanation: The + operator glues strings together.
Real-life example: A bank says "Dear " + customer + "."
School example: A teacher greets: "Hello, " + name.
Home example: A family writes "Happy birthday, " + name.
Nigerian example: A trader sends "Your order of " + qty + " items is ready."
Illustration:
"Hello, " + "Ada" → "Hello, Ada" "Total: " + 5000 → "Total: 5000" "5" + 5 → "55" (careful!)
Mini summary: The + operator joins strings together. Watch out for mixing with numbers.
Definition: Template literals use backticks (`) to make joining strings easier.
Why it is important: They are cleaner than using + many times.
Simple explanation: They let you put values inside a sentence directly.
Real-life example: A bank writes: `Dear ${name}, your balance is ${balance}`.
School example: A teacher writes: `${name} scored ${score}`.
Home example: A family writes: `We have ${count} eggs left.`
Nigerian example: A trader writes: `Your order costs ₦${price}.`
Illustration:
let name = "Ada";
let score = 90;
`Hello, ${name}! Your score is ${score}.`
Mini summary: Template literals use backticks and ${ } for clean strings.
Definition: String methods are built-in tools that work on strings.
Why it is important: They let you change, check, and slice text easily.
Simple explanation: Methods are like tools that do things to your text.
Real-life example: A bank uppercases account numbers.
School example: A teacher finds the length of a student’s name.
Home example: A family trims extra spaces in a shopping item.
Nigerian example: A trader converts a product name to uppercase for invoices.
Illustration:
let name = "Ada";
name.length → 3
name.toUpperCase() → "ADA"
name.toLowerCase() → "ada"
name.charAt(0) → "A"
name.slice(0, 2) → "Ad"
name.includes("d") → true
" Ada ".trim() → "Ada"
Mini summary: String methods help you change, check, and slice text.
Definition: A number is a numeric value used for math and counting.
Why it is important: Numbers are used for scores, prices, ages, and more.
Simple explanation: Numbers can be whole or decimal.
Real-life example: A bank uses numbers for balances.
School example: A teacher uses numbers for scores.
Home example: A family uses numbers for prices.
Nigerian example: A trader uses numbers for quantities and prices.
Illustration:
let age = 13; // whole number let price = 3000.50; // decimal let score = 90; typeof age → "number"
Mini summary: Numbers are used for math and values like age, price, and score.
Definition: Math with numbers uses operators like +, -, *, /.
Why it is important: Websites constantly calculate totals, averages, and more.
Simple explanation: It is the same math you do in school, but the computer does it instantly.
Real-life example: A bank calculates interest with balance * 0.05.
School example: A teacher averages scores with (a + b + c) / 3.
Home example: A family doubles a recipe with cups * 2.
Nigerian example: A trader calculates total with price * quantity.
Illustration:
5 + 3 → 8 10 - 4 → 6 3 * 4 → 12 12 / 3 → 4 2 ** 3 → 8 10 % 3 → 1
Mini summary: Numbers are used with math operators to calculate values.
Definition: Number methods are built-in tools that work on numbers.
Why it is important: They let you round, format, and convert numbers.
Simple explanation: Methods are like tools that help you work with numbers.
Real-life example: A bank formats money to two decimal places.
School example: A teacher rounds scores to whole numbers.
Home example: A family converts a text price into a real number.
Nigerian example: A trader formats prices with commas.
Illustration:
let price = 3000.5678;
price.toFixed(2) → "3000.57"
parseInt("3000") → 3000
parseFloat("3000.50") → 3000.5
Number("500") → 500
Mini summary: Number methods help you format and convert numbers.
Definition: The Math object gives you useful math tools.
Why it is important: You need to round, get random numbers, and find the biggest value.
Simple explanation: Math is a toolbox of math helpers.
Real-life example: A bank rounds interest to the nearest naira.
School example: A teacher finds the highest score in a class.
Home example: A family finds the lowest price in a shop.
Nigerian example: A trader picks a random winner for a raffle.
Illustration:
Math.round(4.6) → 5 Math.floor(4.9) → 4 Math.ceil(4.1) → 5 Math.max(3, 7, 2) → 7 Math.min(3, 7, 2) → 2 Math.random() → 0.0 – 0.9999
Mini summary: The Math object gives you tools to round, find min/max, and get random numbers.
Definition: A boolean is a value that is either true or false.
Why it is important: Booleans control decisions on websites.
Simple explanation: A boolean answers yes/no questions.
Real-life example: A bank checks if a customer has paid fees (true/false).
School example: A teacher checks if a student is present (true/false).
Home example: A family checks if the door is locked (true/false).
Nigerian example: A trader checks if a product is in stock (true/false).
Illustration:
let hasPaidFees = true; let isOpen = false; typeof hasPaidFees → "boolean"
Mini summary: Booleans are true or false values used for decisions.
Definition: Booleans are used in conditions, like if statements.
Why it is important: Every website decision uses a boolean.
Simple explanation: When a boolean is true, one thing happens. When false, another thing happens.
Real-life example: A bank shows "Welcome" if isLoggedIn is true.
School example: A teacher marks "Present" if isHere is true.
Home example: A family locks the door if isNight is true.
Nigerian example: A trader sells if inStock is true.
Illustration:
let isOpen = true;
if (isOpen) {
console.log("Welcome in!");
} else {
console.log("Sorry, we are closed.");
}
Mini summary: Booleans are used to make decisions in code.
Definition: Converting means changing a value from one type to another.
Why it is important: Sometimes a number comes as text, and you need to convert it.
Simple explanation: It is like changing naira to dollars — same value, different form.
Real-life example: A bank converts a typed PIN (string) to a number.
School example: A teacher converts a score from text to number.
Home example: A family converts a typed price to a number before adding.
Nigerian example: A trader converts a WhatsApp price to a number.
Illustration:
Number("500") → 500
String(500) → "500"
Boolean(1) → true
parseInt("500") → 500
parseFloat("50.5") → 50.5
Mini summary: Use Number(), String(), and Boolean() to convert values between types.
Definition: typeof tells you the type of a value.
Why it is important: It helps you avoid bugs by checking data before using it.
Simple explanation: typeof is like asking, "What is this?"
Real-life example: A bank checks if a value is a number before adding interest.
School example: A teacher checks if a score is a number before averaging.
Home example: A family checks if a price is a number before budgeting.
Nigerian example: A trader checks if a quantity is a number before selling.
Illustration:
typeof "Hello" → "string" typeof 42 → "number" typeof true → "boolean" typeof undefined → "undefined" typeof null → "object"
Mini summary: typeof tells you the data type of a value.
Definition: Mistakes happen. Knowing them helps you avoid them.
Why it is important: Mixing types gives wrong results.
Simple explanation: Like adding milk to a shoe — the wrong tool for the job.
Real-life example: A bank adds "500" to 1000 and gets "5001000".
School example: A teacher adds "90" and 10 and gets "9010".
Home example: A family adds "5" eggs and 3 eggs and gets "53".
Nigerian example: A trader adds "3000" and 500 and charges the wrong amount.
Table of common mistakes:
| Mistake | What Happens | How to Fix |
|---|---|---|
| Mixing strings and numbers | Wrong result | Convert with Number() |
| Using quotes on numbers | Text instead of number | Remove quotes |
| Forgetting to convert input | Wrong math | Use parseInt/parseFloat |
| Comparing string "5" to number 5 | Wrong equality | Use === or convert |
| Using booleans in math | Unexpected numbers | Keep booleans for logic |
| Assuming text is a number | NaN errors | Check with typeof |
Mini summary: Common mistakes: mixing types, forgetting to convert, and using quotes on numbers.
Definition: Best practices are good habits that make code clean and safe.
Why it is important: Good habits prevent bugs and help others read your code.
Simple explanation: Like keeping your tools sharp and organized.
Real-life example: A bank writes clean, safe code.
School example: A student writes neat, readable code.
Home example: A family keeps a tidy shopping list.
Nigerian example: A business writes clear, easy-to-maintain code.
List of best practices:
toFixed when showing money.Math.round when displaying whole numbers.Mini summary: Best practices: template literals, type conversion, and clean code.
You now know how to work with strings, numbers, and booleans.
What you learned:
Next steps: In Module Three, you will learn about objects, arrays, and special types.
Illustration:
Your Learning Journey:
Module 1: Foundations
|
V
Module 2: Strings, Numbers, Booleans
|
V
Module 3: Objects, Arrays, Special Types
|
V
Module 4: Conversion, Scope & Certification
|
V
Data Types Expert 🎉
Mini summary: You now have a full toolkit for the three everyday data types.
| Word | Simple Definition |
|---|---|
| String | Text inside quotes. |
| Number | A numeric value like 5 or 3.14. |
| Boolean | A true or false value. |
| Template literal | A string using backticks and ${ }. |
| Method | A built-in tool that works on a value. |
| Concatenation | Joining strings with +. |
| parseInt | Converts a string to a whole number. |
| parseFloat | Converts a string to a decimal number. |
| toFixed | Rounds a number to a set number of decimals. |
| Math | A built-in object of math helpers. |
| typeof | An operator that tells the type of a value. |
| NaN | "Not a Number" — an invalid number value. |
| Type conversion | Changing a value from one type to another. |
| camelCase | A naming style like totalSales. |
| Console | A tool for testing JavaScript. |
let name = "Ada";.console.log(name).name.toUpperCase().name.length.`.${value} inside.let a = 10; and let b = 5;.a + b, a * b, etc.let price = 3000.5678;.price.toFixed(2)."3000.57".let isOpen = true;.isOpen.false to see the other result.toFixed(2).petName.toUpperCase().Math.random() for random dice.toFixed(2).toFixed(2).String: "Ada" → text Number: 42, 3.14 → math Boolean: true, false → decisions
"Hello, " + "Ada" → "Hello, Ada"
`Hello, ${name}!`
Math.round(4.6) → 5 Math.floor(4.9) → 4 Math.ceil(4.1) → 5
Module 1: Foundations
|
V
Module 2: Strings, Numbers, Booleans
|
V
Module 3: Objects, Arrays, Special Types
|
V
Module 4: Conversion, Scope & Certification
|
V
Data Types Expert 🎉
| Feature | String | Number |
|---|---|---|
| Example | "Ada" | 42 |
| Quotes | Yes | No |
| Math | No | Yes |
| Length | Yes | No |
| Feature | Number | Boolean |
|---|---|---|
| Values | Any number | true or false |
| Use | Math | Decisions |
| Example | 42 | true |
| Method | What it does |
|---|---|
| length | Counts characters |
| toUpperCase() | Makes all caps |
| toLowerCase() | Makes all small |
| slice() | Cut a piece |
| includes() | Check for text |
| trim() | Remove extra spaces |
| Conversion | Example | Result |
|---|---|---|
| String → Number | Number("500") | 500 |
| Number → String | String(500) | "500" |
| Any → Boolean | Boolean(1) | true |
| Text → Integer | parseInt("500") | 500 |
| Text → Decimal | parseFloat("50.5") | 50.5 |
Lesson 1: Strings are text inside quotes.
Lesson 2: + joins strings together.
Lesson 3: Template literals use backticks and ${ }.
Lesson 4: String methods change, check, and slice text.
Lesson 5: Numbers are used for math and counting.
Lesson 6: Math operators calculate values.
Lesson 7: Number methods format and convert numbers.
Lesson 8: The Math object gives rounding, min/max, and random.
Lesson 9: Booleans are true or false.
Lesson 10: Booleans are used in decisions.
Lesson 11: Convert types with Number(), String(), Boolean().
Lesson 12: typeof tells the type of a value.
Lesson 13: Common mistakes: mixing types, missing conversion.
Lesson 14: Best practices: template literals, conversion, clean code.
Lesson 15: You now have a full toolkit for the three everyday data types.
Congratulations! You have finished Module Two of the Variables & Data Types for Web course. You learned how to use strings, numbers, and booleans. You learned how to join strings with + and template literals. You learned string methods like length, toUpperCase, and slice. You learned how to do math with numbers and use number methods like toFixed, parseInt, and parseFloat. You learned about the Math object for rounding and random numbers. You learned about booleans and how to use them in decisions. You learned how to convert between types and check types with typeof. You learned common mistakes and best practices. Most importantly, you can now work confidently with the three most common data types on the web. In the next module, you will learn about objects, arrays, and special types.
toFixed(2).Match the term to its meaning.
| Term | Meaning |
|---|---|
| 1. String | A. A true or false value |
| 2. Number | B. Text inside quotes |
| 3. Boolean | C. A built-in math tool |
| 4. Math | D. A numeric value |
| 5. typeof | E. Tells the type of a value |
Answers: 1-B, 2-D, 3-A, 4-C, 5-E
Title: “Build a Small Bill Calculator Together”
Instructions: In groups of 3–4, create variables for two items (name, price, quantity). Calculate the total using numbers. Format the total with toFixed(2). Show a friendly message with template literals. Present to the class.
Goal: Practice strings, numbers, and template literals.
Task: In the browser console, create:
Write down each and its result.
Project: “My First Shop Receipt”
Create a JavaScript receipt with:
Example:
let shopName = "Sweet Lagos Bakery";
let item1 = "Cake", price1 = 5000, qty1 = 2;
let item2 = "Bread", price2 = 1000, qty2 = 3;
let discountApplied = true;
let total = (price1 * qty1) + (price2 * qty2);
if (discountApplied) total -= 500;
console.log(`${shopName} — Total: ₦${total.toFixed(2)}`);
Assignment: Ask a family member about a small business. Then write JavaScript that:
toFixed(2).Submit: Your code and a short explanation.
toFixed(2) do?In Module Three, we will learn about objects, arrays, and special types. We will cover:
To prepare, complete the practical assignment and review the key vocabulary. Practice a few string and number examples in the console. Bring your curiosity!
See you in Module Three!
End of Module Two – Variables & Data Types for Web