In the previous tutorial, you learned what JavaScript is and how to run your first script. Now it’s time to learn about JavaScript Variables — containers that store data your program can use and change.
What is a Variable?
Think of a variable as a labeled box. You put a value inside it, give it a name, and later you can open that box to read or change what’s inside. In JavaScript, you create variables using three keywords: var, let, and const.
let studentName = "Ali";
console.log(studentName);
Here, studentName is the box, and "Ali" is what’s stored inside it.
JavaScript Variables: var, let, and const
JavaScript gives you three ways to declare a variable. Each behaves a little differently, and understanding the difference will save you from confusing bugs later.
1. var (old way — avoid in new code)
var city = "Karachi";
console.log(city);
var city = "Lahore"; // allowed — can be redeclared
console.log(city);
var was the only way to declare variables before 2015. It can be redeclared and has confusing scope rules, so modern JavaScript avoids it. We’re showing it here only so you recognize it in older code.
2. let (modern, changeable)
let age = 25;
console.log(age);
age = 26; // allowed — value can change
console.log(age);
Use let when you know the value will change later, like a counter, a score, or user input.
3. const (modern, fixed)
const pi = 3.14;
console.log(pi);
pi = 3.15; // Error! Cannot reassign a const
Use const when the value should never change, like a fixed price, a configuration value, or a constant like pi.
Rules for Naming Variables
- Must start with a letter, underscore (_), or dollar sign ($)
- Cannot start with a number
- Case-sensitive (
ageandAgeare different variables) - Cannot use reserved words (like
let,const,function)
let studentAge = 20; // valid
let student_age = 20; // valid
let $price = 100; // valid
let 2ndName = "Ali"; // invalid — starts with a number
Practical Example: Student Profile
Let’s combine everything into one real example you’ll actually use:
const studentName = "Ayesha";
let studentAge = 22;
let studentCity = "Karachi";
console.log("Name: " + studentName);
console.log("Age: " + studentAge);
console.log("City: " + studentCity);
studentAge = 23; // age can change next year
console.log("Updated Age: " + studentAge);
Notice studentName uses const because a name typically won’t change, while studentAge uses let because it updates every year.
Quick Recap
- Variables store data your program can use
varis old and best avoided in new codeletis for values that can changeconstis for values that stay fixed
That wraps up JavaScript Variables — a core building block you’ll use in every single JavaScript program you write from here on.
Practice Exercise
- Create a
constvariable for your name and print it - Create a
letvariable for your age, print it, then change it and print again - Try reassigning a
constvariable and observe the error in the console
Bonus Challenge
Create 4 variables for a product: productName (const), price (let), quantity (let), and inStock (const, true/false). Print all four using console.log(), then update price and quantity and print them again.
What’s Next?
In the next tutorial, we’ll cover JavaScript Data Types — where you’ll learn exactly what kind of values (numbers, strings, booleans) you can store in these variables.
Missed the last tutorial? Go back to JavaScript Introduction, or browse the full JavaScript Tutorial Series.

