Skip to main content

What are variables?

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.

Declaring variables

// Use const for values that won't change
const apiUrl = "https://api.example.com";
const maxRetries = 3;
const user = { name: "Sarah", email: "sarah@example.com" };

// Use let for values that will change
let 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.

Updating variables

let count = 0;
console.log(count); // 0

count = 5;
console.log(count); // 5

count = count + 1;
console.log(count); // 6

// Shorthand
count += 10;
console.log(count); // 16
With let, you can reassign the variable as many times as needed. Use +=, -=, *=, /= for arithmetic shortcuts.

Naming conventions

// Use camelCase for variable names
const firstName = "Sarah";
const userEmail = "sarah@example.com";
const isLoggedIn = true;
const totalPrice = 99.99;

// Use UPPER_CASE for constants that never change
const 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.

Variable scope

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.

Comparing to Python

// JavaScript uses const/let
const name = "Sarah";
let age = 25;

age = 26; // Can reassign let
Python doesn’t distinguish between changeable and unchangeable variables - everything can be reassigned. JavaScript’s const helps you catch bugs by preventing accidental reassignments.

Common mistakes

// ❌ Wrong: Can't reassign const
const name = "Sarah";
name = "John"; // TypeError: Assignment to constant variable

// ✅ Correct: Use let if you need to reassign
let 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.
// This is actually OK with const:
const user = { name: "Sarah", age: 25 };
user.age = 26; // Modifying a property works
user.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 works
numbers = [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.
// ❌ Wrong: var has confusing scoping rules
var count = 0;

// ✅ Correct: Always use let or const
let 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.

What’s next?

Now that you can store data in variables, let’s learn about the different types of data JavaScript can handle.

Data types

Numbers, strings, booleans, and more