Lesson 18: Variables and Data Types
📦 Lesson 18: Variables and Data Types
A variable in JavaScript is like a labeled box. You can store something inside it — a name, a number, a state — and pull it out whenever you need it.
🔑 Declaring Variables
There are three main ways to declare variables in JavaScript:
let name = "Joe";
const age = 25;
var isStudent = true;
let: Used for variables that can change later. This is the most common choice today.const: Use this when you don’t plan to reassign the variable. It’s a constant.var: The old-school way. It still works but has weird scoping behavior. Avoid it unless you know what you’re doing.
🎨 Data Types in JavaScript
JavaScript supports several basic data types. Here’s a breakdown with examples:
1. String
Text values surrounded by quotes.
let city = "Lagos";
2. Number
Just numbers — integers or decimals.
let score = 98;
let price = 14.99;
3. Boolean
Either true or false. Used for logic, conditionals, etc.
let isLoggedIn = true;
let hasPaid = false;
4. Null
A deliberate “nothing.” You assign it when you want to empty a variable.
let user = null;
5. Undefined
A variable that exists but hasn’t been given a value yet.
let notSet;
console.log(notSet); // undefined
6. Object
A collection of key-value pairs. Think of it like a dictionary or real-world object.
let person = {
name: "Joe",
age: 25,
isStudent: true
};
7. Array
A list of items stored in a single variable. Each item has an index (starting from 0).
let fruits = ["apple", "banana", "orange"];
📌 Tip: Type Checking
You can check the type of a variable using the typeof operator:
typeof "hello" // "string"
typeof 10 // "number"
typeof false // "boolean"
typeof undefined // "undefined"
typeof null // "object" (weird quirk)
typeof {} // "object"
typeof [] // "object"
🚀 What’s Next?
Now that you understand how data is stored and the types of data JavaScript uses, we’ll move on to Conditionals and loops — the building blocks of interaction.