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
- Introduction to Programming & JavaScript
- Your First JavaScript Program
- Variables — Your Data Containers
- Data Types — Kinds of Values
- Operators & Expressions
- Conditional Statements
- Loops & Iterations
- Functions
- Arrays — Lists of Data
- Objects & Classes
- Strings — Working with Text
- Error Handling
- Mini-Projects & Practice