Learn how to store and work with different types of information
in JavaScript!
What are Data Types?
Data types tell JavaScript what kind of information you're
working with. Just like in real life, we handle text differently from numbers - you can't add "hello" and
"world" mathematically!
JavaScript has several main data types that you'll use every
day:
💡 Think of it this way: Data types are like categories - foods, numbers, yes/no answers.
Each category has different rules!
Key Concepts
💬
String
Text data - words, sentences, any characters in quotes.
🔢
Number
Numeric values - whole numbers and decimals for math.
✅
Boolean
True or false values - perfect for yes/no decisions!
📋
Array
Lists of items - store multiple values in one variable.
Changing data from one type to another when
needed.
String - Working with Text
💬 Text in JavaScript
let studentName = "Ahmad";
let greeting = 'Selamat pagi!';
let city = "Kuala Lumpur";
console.log(studentName); // Ahmad
console.log(greeting + " " + studentName); // Selamat pagi! Ahmad
Important: Strings can use "double
quotes" or 'single quotes' - just be consistent!
Number - Working with Math
🔢 Numbers in JavaScript
let age = 13; // Whole number
let price = 15.50; // Decimal number
let temperature = -5; // Negative number
let total = price * 3; // Math with numbers
console.log(total); // 46.5
Note: Numbers don't use quotes! Compare:
"42" is text, 42 is a number.
Boolean - True or False
✅ Boolean Values
let isRaining = true;
let hasHomework = false;
let schoolOpen = true;
console.log(isRaining); // true
console.log(hasHomework); // false
Use cases: Booleans are perfect for
tracking status, making decisions, and conditions!
Array - Lists of Items
📋 Storing Multiple Items
let fruits = ["rambutan", "mangosteen", "durian"];
let scores = [95, 87, 92, 78];
let mixed = ["Ahmad", 13, true, "Kuala Lumpur"];
console.log(fruits[0]); // rambutan (first item)
console.log(scores[1]); // 87 (second item)
console.log(fruits.length); // 3 (number of items)
Key facts: Arrays use [ ] brackets,
items are separated by commas, counting starts at 0!
Variable Naming Rules (camelCase)
🏷️ Good Variable Names
// Good variable names (camelCase)
let userName = "Siti";
let totalPrice = 50.00;
let isStudentActive = true;
let shoppingCartItems = ["book", "pen", "notebook"];
// Bad variable names - avoid these!
let username = "Siti"; // Not camelCase
let total_price = 50.00; // Use camelCase, not snake_case
let x = true; // Not descriptive
let 1student = "Ahmad"; // Can't start with number!
Rules: Start with lowercase, capitalize
each new word, be descriptive, no spaces or special characters!
Type Conversion
🔄 Converting Between Types
// String to Number
let textNumber = "42";
let realNumber = Number(textNumber);
console.log(realNumber + 8); // 50
// Number to String
let age = 13;
let message = "I am " + String(age) + " years old";
console.log(message); // I am 13 years old
// Be careful with automatic conversion!
console.log("5" + 3); // "53" (string joining)
console.log(Number("5") + 3); // 8 (number addition)
// From my phone
let contactName = "Mom"; // String
let unreadMessages = 5; // Number
let wifiConnected = true; // Boolean
let recentApps = ["WhatsApp", "YouTube", "Instagram"]; // Array
🏆 Bonus Challenge: Find 3 examples where the wrong data type would cause problems! (Like
trying to multiply someone's name)
💪 Practice Challenges
Challenge 1: Food Calculator
Create variables and calculate the total cost:
let chickenRice = 6.50;
let icedMilo = 3.00;
let curryPuff = 1.50;
// Calculate total and add 6% tax
Expected: Calculate subtotal, tax,
and final total
Challenge 2: Student Profile
Create a complete student profile using all data types:
let studentName = ""; // Add your name
let studentAge = 0; // Add your age
let isPresent = true; // Are you in class?
let favoriteSubjects = []; // List 3 subjects
// Display all information
Challenge 3: Malaysian Cities Array
Create and manipulate an array:
let cities = ["Kuala Lumpur", "Penang", "Johor Bahru", "Malacca"];
// Display the first city
// Display the last city
// Display total number of cities
// Add "Kota Kinabalu" to the list
Challenge 4: E-Wallet Balance
Simulate e-wallet transactions:
let walletBalance = 100.00;
let coffeePrice = 5.50;
let lunchPrice = 12.00;
// Subtract purchases from balance
// Display remaining balance with message
Challenge 5: Type Conversion Challenge
Fix this code by converting types correctly:
let userInput = "25"; // This is a string
let bonus = 10; // This is a number
// We want to add them: should be 35, not "2510"
// Fix the code!
let total = userInput + bonus;
console.log(total);