Variables store data that you’ll use throughout your program - user input, API responses, calculation results, and more.JavaScript has two ways to declare variables: let for values that change, and const for values that don’t.
// Use const for values that won't changeconst apiUrl = "https://api.example.com";const maxRetries = 3;const user = { name: "Sarah", email: "sarah@example.com" };// Use let for values that will changelet userInput = "";let attemptCount = 0;let isLoading = false;
const prevents reassignment - once you set a value, you can’t change it. let allows the value to be updated later.
Default to const unless you know the value needs to change. This prevents accidental reassignments and makes your code easier to understand.
// Use camelCase for variable namesconst firstName = "Sarah";const userEmail = "sarah@example.com";const isLoggedIn = true;const totalPrice = 99.99;// Use UPPER_CASE for constants that never changeconst API_KEY = "abc123xyz";const MAX_FILE_SIZE = 5000000;const DEFAULT_TIMEOUT = 30000;
Start with a lowercase letter, then capitalize the first letter of each new word. For true constants (values that never change across your entire app), use UPPER_CASE with underscores.
JavaScript variable names can contain letters, numbers, underscores, and dollar signs - but they can’t start with a number.
const globalVar = "I'm available everywhere";function myFunction() { const functionVar = "I only exist inside this function"; if (true) { const blockVar = "I only exist inside this if block"; let anotherBlockVar = "Me too!"; console.log(globalVar); // Works console.log(functionVar); // Works console.log(blockVar); // Works } console.log(blockVar); // Error: blockVar is not defined}
Variables declared with const and let are “block-scoped” - they only exist within the nearest set of curly braces {}.
Keep variables in the smallest scope possible. This makes your code easier to debug and prevents naming conflicts.
// JavaScript uses const/letconst name = "Sarah";let age = 25;age = 26; // Can reassign let
Copy
# Python doesn't have constname = "Sarah"age = 25age = 26 # Can always reassign
Python doesn’t distinguish between changeable and unchangeable variables - everything can be reassigned. JavaScript’s const helps you catch bugs by preventing accidental reassignments.
// ❌ Wrong: Can't reassign constconst name = "Sarah";name = "John"; // TypeError: Assignment to constant variable// ✅ Correct: Use let if you need to reassignlet name = "Sarah";name = "John"; // Works fine
const prevents reassignment, but it doesn’t make objects or arrays immutable. You can still modify properties or add items - you just can’t reassign the entire variable.
Thinking const makes objects immutable
Copy
// This is actually OK with const:const user = { name: "Sarah", age: 25 };user.age = 26; // Modifying a property worksuser.email = "sarah@example.com"; // Adding a property works// This is what const prevents:user = { name: "John" }; // TypeError: Assignment to constant variable// Same with arrays:const numbers = [1, 2, 3];numbers.push(4); // Adding to array worksnumbers = [5, 6, 7]; // TypeError: Assignment to constant variable
const only prevents reassigning the variable itself. You can still modify the contents of objects and arrays.
Using var instead of let/const
Copy
// ❌ Wrong: var has confusing scoping rulesvar count = 0;// ✅ Correct: Always use let or constlet count = 0;
var is the old way of declaring variables. It has weird scoping rules and can cause bugs. Pretend it doesn’t exist - always use let or const.