Curriculum / Fundamentals / Lesson 02

Variables in JavaScript

Learn how to store and manage data using let, const, and var.

2 of 30
beginner 📖 6 min read 🎯 4 questions

What is a Variable?

A variable is like a labeled box that stores a value. Instead of writing the number 25 everywhere in your code, you store it once and refer to it by name. When the value needs to change, you change it in one place.

💡 Real-world analogy Think of a variable as a sticky note with a label. The label is the variable name, and what you write on the note is the value.

Three Ways to Declare Variables

JavaScript gives you three keywords to create variables:

let — The Default Choice

const — For Values That Don't Change

var — The Old Way (Avoid This)

Rule of thumb Default to const. Switch to let only when you know the value needs to change. Never use var in new code.

Variable Naming Rules

⚠️ Common mistake Trying to reassign a const variable throws a TypeError. If you get this error, the variable probably should have been let.

Quick Quiz

1. Which keyword should you use for a value that will never change?

2. Which of these is a valid variable name?

3. What happens when you try to reassign a const variable?

Summary

Back to curriculum