Ch 3 · Variables — Your Data Containers · JavaScript

Topic 3 of 13 in JavaScript — Complete Tutorial (Beginner) — 5 lessons.

What is a Variable?

Imagine you are playing a video game and your character earns points. The game needs to remember your score at all times — it needs somewhere to store that number. In programming, we use a variable for exactly this. A variable is a named storage location in the computer's memory that holds a piece of data. You give it a name, you put a value inside it, and you can read or change that value any time your program needs to.

The Three Keywords: let, const, and var

JavaScript gives you three keywords to create variables, but they behave differently. Understanding when to use each one is one of the most important habits you will build as a programmer. The simplest rule to remember: use const by default, and only switch to let when you know the value will need to change.

Declaring, Assigning, and Updating Variables

There are three actions you perform with variables throughout your program. The first is declaration — this is when you create the variable for the first time using let or const. The second is assignment — putting a value into the variable using the = sign. In most cases, you do both at the same time in one line. The third action is updating — changing the value of a let variable later in your program.

Naming Conventions

Choosing good names for your variables is one of the most important skills in programming. The name of a variable should immediately tell anyone reading your code what the variable contains — without them needing to look at the value. A name like x or n or temp tells you nothing. A name like numberOfStudents or isRaining tells you exactly what is stored there.

Variables in Action: A Full Example

Let us bring everything together in a real mini-program that uses multiple variables the way you would in an actual project. Notice how the variable names make the code read almost like plain English — that is the goal.

All topics in JavaScript Beginner

  1. Introduction to Programming & JavaScript
  2. Your First JavaScript Program
  3. Variables — Your Data Containers
  4. Data Types — Kinds of Values
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Arrays — Lists of Data
  10. Objects & Classes
  11. Strings — Working with Text
  12. Error Handling
  13. Mini-Projects & Practice