In JavaScript, variables are containers for storing data values. Think of them as labeled boxes. Data Types describe the kind of data you are putting inside those boxes.
1. Variables (const, let, var)
const: Use this for values that won't change. It stands for "constant." (Best practice: use this by default).let: Use this for values that will change later (like a score in a game).var: The old way of doing things. It’s generally avoided in modern code because it has confusing "scoping" rules.
2. Data Types
String: Text wrapped in quotes (e.g.,
"Hello").Number: Integers or decimals (e.g.,
25,3.14).Boolean: Logical values—either
trueorfalse.Null/Undefined: Represents "nothing" or an empty value.
// 1. Declaring Variables
const birthYear = 1995; // This cannot be changed
let currentAge = 28; // This can be updated
var oldSchool = "I'm var"; // The legacy way
// 2. Updating a variable
currentAge = 29;
// 3. Different Data Types
const name = "Alice"; // String
const price = 19.99; // Number
const isOnline = true; // Boolean
let userSection; // Undefined (box is empty)
let trophy = null; // Null (explicitly empty)
// 4. Checking types using 'typeof'
console.log(typeof name); // Output: "string"
console.log(typeof price); // Output: "number"