๐ฏ Learning Objectives
- Understand var, let, and const
- Learn JavaScript data types
- Work with strings, numbers, and booleans
- Create and manipulate arrays
- Practice variable declaration and usage
๐ What is JavaScript?
JavaScript is a programming language that makes websites interactive. It runs in the browser and can respond to user actions.
๐ค Variables: Storing Data
Variables are containers for storing data values.
Three Ways to Declare Variables
// var - old way (function-scoped)
var name = "John";
// let - modern way (block-scoped, can be reassigned)
let age = 25;
age = 26; // Can change
// const - modern way (block-scoped, cannot be reassigned)
const country = "Nigeria";
// country = "USA"; // Error! Cannot reassign const
When to Use Each?
- const: Use by default (values that won't change)
- let: Use when value will change
- var: Avoid in modern JavaScript
๐ Data Types
1. String (Text)
let firstName = "Ahmad";
let lastName = 'Garba';
let fullName = "Ahmad Tijjani";
// String concatenation
let greeting = "Hello, " + firstName + "!";
console.log(greeting); // Hello, Ahmad!
// Template literals (modern way)
let message = `Welcome ${firstName} ${lastName}`;
console.log(message); // Welcome Ahmad Garba
2. Number
let age = 25;
let price = 99.99;
let temperature = -5;
// Math operations
let sum = 10 + 5; // 15
let difference = 20 - 8; // 12
let product = 4 * 3; // 12
let quotient = 15 / 3; // 5
let remainder = 10 % 3; // 1 (modulus)
3. Boolean (True/False)
let isStudent = true;
let hasGraduated = false;
let isLoggedIn = true;
console.log(isStudent); // true
4. Array (List of Values)
// Creating arrays
let fruits = ["Apple", "Banana", "Orange"];
let numbers = [1, 2, 3, 4, 5];
let mixed = ["text", 42, true];
// Accessing array elements (index starts at 0)
console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana
console.log(fruits[2]); // Orange
// Array length
console.log(fruits.length); // 3
// Adding elements
fruits.push("Mango"); // Add to end
console.log(fruits); // ["Apple", "Banana", "Orange", "Mango"]
// Removing elements
fruits.pop(); // Remove last element
console.log(fruits); // ["Apple", "Banana", "Orange"]
๐ป Console.log() - Printing Output
// Print to browser console
console.log("Hello, World!");
// Print variables
let name = "Sarah";
console.log(name);
// Print multiple values
console.log("Name:", name, "Age:", 23);
๐งช Complete Practice Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Variables Practice</title>
</head>
<body>
<h1>JavaScript Variables Practice</h1>
<p>Open browser console (F12) to see output</p>
<script>
// Strings
const firstName = "Ahmad";
const lastName = "Garba";
const fullName = firstName + " " + lastName;
console.log("Full Name:", fullName);
// Numbers
const age = 25;
const nextYearAge = age + 1;
console.log("Age:", age);
console.log("Next year:", nextYearAge);
// Booleans
const isStudent = true;
const hasJob = false;
console.log("Is Student?", isStudent);
console.log("Has Job?", hasJob);
// Arrays
const skills = ["HTML", "CSS", "JavaScript"];
console.log("My Skills:", skills);
console.log("First Skill:", skills[0]);
console.log("Number of skills:", skills.length);
// Add new skill
skills.push("Bootstrap");
console.log("Updated Skills:", skills);
// Math operations
const price1 = 100;
const price2 = 50;
const total = price1 + price2;
console.log("Total Price:", total);
// Template literals
const message = `My name is ${fullName} and I am ${age} years old.`;
console.log(message);
</script>
</body>
</html>
โ๏ธ Practice Exercises
Your Turn to Code!
- Create variables for your name, age, and city
- Create an array of your favorite foods (at least 5)
- Calculate: (10 + 5) * 3 and store in variable
- Use console.log to print all variables
- Add two more items to your food array
โ Today's Deliverable
- variables-practice.html with JavaScript code
- Variables using const and let
- At least 5 string variables
- At least 5 number variables
- At least 2 arrays
- All output visible in console
- Screenshot of console output