Variables & Data Lesson 5 of 30
JavaScript Variables — let, const and var
Store values in variables with let and const, change them, and learn why modern JavaScript avoids var. Includes a baon budget example.
A variable is a labeled box where your program keeps a value: a name, a score, a price. You give the box a name, put a value inside, and use the name whenever you need the value.
Creating a variable with let
<p id="out"></p>
<script>
let name = "Juan";
let grade = 10;
let baon = 150;
document.getElementById("out").textContent =
name + " is in Grade " + grade + " and has ₱" + baon + " baon.";
</script>
let name = "Juan"; means: create a variable called name and put the text "Juan" inside it. The = sign here does not mean "equals" as in math; it means "store this value". Programmers call it the assignment operator.
Changing a value
A let variable can change. Don't write let again; just assign a new value:
<pre id="out"></pre>
<script>
let baon = 150;
let log = "Start: ₱" + baon + "\n";
baon = baon - 45; // lunch
log += "After lunch: ₱" + baon + "\n";
baon = baon - 20; // jeepney fare
log += "After fare: ₱" + baon + "\n";
document.getElementById("out").textContent = log;
</script>
baon = baon - 45 looks strange in math, but in code it means: take the current value of baon, subtract 45, and store the result back in baon.
const: values that never change
Use const (short for constant) for values that should stay the same:
const SCHOOL = "Rizal National High School";
const PASSING_GRADE = 75;
const DAYS_IN_A_WEEK = 7;
If you try to change a const, JavaScript stops with an error:
const PASSING_GRADE = 75;
PASSING_GRADE = 60; // TypeError: Assignment to constant variable.
What about var?
Old JavaScript only had var. It still works, but it has confusing rules: it ignores some code blocks, and you can create the same variable twice by accident. Modern code uses let and const. If you see var in an old tutorial, you can usually replace it with let.
Declaring without a value
You can create a let variable now and give it a value later. Until then, its value is undefined (meaning "nothing stored yet"):
let winner;
console.log(winner); // undefined
winner = "Section Rizal";
console.log(winner); // Section Rizal
A const must get its value immediately: const x; alone is an error.
Many variables at once
let math = 88, science = 91, english = 85;
This works, but one variable per line is easier to read and comment.
Good variable names
- Use names that say what's inside:
totalPrice, nottporx. - Use camelCase:
studentCount,isPassing. - Constants that are fixed settings are often written in ALL_CAPS:
MAX_STUDENTS. - English names are standard in code, even if your comments are in Filipino.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04