45 Mins beginner
2. Variables & Data Types
Fundamentals
Variables & Data Types
Variables are containers for storing data. Historically, JavaScript used var to declare variables, but modern JavaScript uses let and const to prevent unexpected bugs.
📦 let vs const
Use let for values that will change later. Use const for values that should remain locked forever. Default to const unless you know the value must change.
let score = 0;
score = 10;
const playerName = "Alice";
console.log(score);
console.log(playerName);
Output10
Alice
Alice
⚠️TypeError: Attempting to reassign a
const variable will instantly crash your program.🧩 Primitive Data Types
JavaScript has several core data types. It is dynamically typed, meaning the engine figures out the type automatically.
- String: Text wrapped in quotes.
- Number: Both integers and decimals.
- Boolean:
trueorfalse. - Undefined: A variable that has been declared but not assigned a value.
- Null: An intentional absence of any value.
const username = "JohnDoe";
const age = 25;
const price = 19.99;
const isOnline = true;
let subscription;
const emptySlot = null;
console.log(typeof age);
console.log(typeof isOnline);
console.log(subscription);
Outputnumber
boolean
undefined
boolean
undefined
Knowledge Check
Ready to test your understanding of 2. Variables & Data Types?