Variables in JavaScript
Variables are one of the most important concepts in any programming language. A variable is simply a container used to store information. You can save values like numbers, text, or even complex data, and use them whenever needed in your program.
As a college student or fresher, understanding variables properly will help you write better code and avoid common beginner mistakes.
What Is a Variable?
A variable works like a storage box.
You assign a name to the box, and then you put a value inside it.
For example:
let age = 21;
Here:
age? the variable name21? the value stored inside it
You can later use age anywhere in your program.
Why Do We Use Variables?
Variables make your code flexible and reusable.
You use variables to:
Store user data
Save calculations
Hold temporary values
Manage text, numbers, or details
Build dynamic applications
Without variables, your code would be fixed and limited.
How to Create a Variable
JavaScript gives you three ways to declare variables:
varletconst
They look similar but behave differently.
Using var (Old Way)
var was used in older JavaScript versions. It still works but has some problems with scope, which we will learn later.
Example:
var city = "Delhi";
console.log(city);
Output:
DelhiYou should avoid using var in modern JavaScript unless required.
Using let (Modern and Recommended)
let is the most commonly used keyword to create variables because it is safe and predictable.
Example:
let name = "Karan";
console.log(name);
Output:
KaranYou can change the value of a let variable later.
Example:
let score = 50;
score = 75; // value updated
console.log(score);
Output:
75
This makes let perfect for values that change.
Using const (For Values That Never Change)
const is used when you do not want the value to change.
Example:
const pi = 3.14;
console.log(pi);
Output:
3.14
If you try to change it:
pi = 3.99;
You will get an error because const values cannot be updated.
Difference Between var, let, and const
| Keyword | Can Update Value? | Good For |
|---|---|---|
| var | Yes | Older code, not recommended |
| let | Yes | Most variables in modern code |
| const | No | Fixed values |
In real-world applications, developers mostly use let and const.
Rules for Naming Variables
When naming variables:
Use letters, numbers, underscore, or dollar sign
Names cannot start with a number
Use simple and meaningful names
Avoid special characters
Use camelCase (recommended)
Examples of valid names:
let firstName;
let student_age;
let totalMarks;
Examples of invalid names:
let 1name; // cannot start with numberlet user-name; // hyphen not allowedStoring Different Types of Data
Variables can store different types of values.
Example:
let age = 20; // numberlet student = "Riya"; // stringlet isPassed = true; // booleanlet marks = [50, 60, 70]; // arraylet details = { name: "Riya", age: 20 }; // objectYou will learn all these types in the next chapters.
Example Program With Output
let studentName = "Arjun";
let rollNumber = 101;
let isActive = true;
console.log("Student Name:", studentName);
console.log("Roll Number:", rollNumber);
console.log("Active:", isActive);
let marks = 45 + 35 + 40;
console.log("Total Marks:", marks);
const collegeName = "ABC College";
console.log("College:", collegeName);
Output:
Student Name: ArjunRoll Number: 101Active: trueTotal Marks: 120College: ABC CollegeThis example shows how different variables are used in real programs.
Common Mistakes Beginners Make
Using
varinstead ofletChanging a
constvalueUsing confusing names like
x1,temp, etc.Forgetting to declare variables
Using spaces in variable names
Avoid these mistakes to write clean and error-free code.
Practice Task (Do It Yourself)
Create variables for:
Your name
Your branch or course
Your favorite sport
Your age
Your current city
Print all values in the console.