Understanding Variables and Data Types in JavaScript

Every application you use, whether it's Instagram, YouTube, or a simple calculator, stores information somewhere. Your username, your age, whether you are logged in or not, all of this information needs to be stored somewhere in the program. In JavaScript, we store this information using variables.
In this blog we will understand what variables are and why they are needed, how to declare variables using var, let, and const, what primitive data types are, the basic difference between var, let, and const, and what scope means in a very simple way.
First, let's understand what variables are and why they are needed.
What are Variables and Why are They Needed
Think of a variable like a box. You can put something inside that box, give the box a label, and whenever you need that thing, you just look for the box with that label.
In JavaScript, a variable is a named box that stores a value. Instead of writing the same value again and again, you store it once in a variable and use the variable name wherever you need it.
For example, if your name is Suprabhat instead of writing "Suprabhat" everywhere in your code, you store it once:
let name = "Suprabhat";
Now whenever you need the name, just use the variable name. If the name changes tomorrow, you only update it in one place.
Without variables, writing programs would be very difficult and messy.
How to Declare Variables using var, let, and const
In JavaScript, there are three ways to create a variable.
Using var
var is the oldest way to declare a variable in JavaScript. You can change its value later and it can be used anywhere in your code.
var city = "Delhi";
city = "Mumbai"; // value changed, this works fine
Using let
let is the modern and recommended way to declare a variable whose value can change later.
let age = 20;
age = 21; // value updated, this works fine
Using const
const is used when the value should never change. Once you set a value, it stays fixed forever.
const country = "India";
country = "USA"; // Error! You cannot change a const value
Use const when you know the value will not change, like a country name or a fixed tax rate.
Primitive Data Types in JavaScript
Every value stored in a variable has a type. JavaScript has five basic data types that you will use all the time.
- String
A string is any text value. It is always written inside quotes.
let name = "Aryan"; // this is a string
let city = "Bangalore"; // this is also a string
- Number
A number is any numeric value, whole numbers or decimal numbers.
let age = 20; // whole number
let price = 99.5; // decimal number
- Boolean
A boolean has only two possible values, true or false. It is used to represent yes/no situations.
let isStudent = true;
let isLoggedIn = false;
- Null
Null means the value is intentionally empty. You set it to null when you want to say "there is nothing here right now."
let score = null; // no score assigned yet
- Undefined
Undefined means a variable has been declared but no value has been given to it yet.
let address;
console.log(address); // undefined
In short null is an empty box you placed there on purpose, and undefined is a box that exists but nobody put anything inside it yet.
Basic Difference between var, let, and const
Here is a simple way to understand the difference between all three.
Can the value change?
With var — yes, you can change the value. With let — yes, you can change the value. With const — no, the value is fixed forever.
Where can it be used?
var can be used anywhere in the function, even outside the block where it was created. This can sometimes cause unexpected bugs.
let and const are more controlled. They only exist inside the block where they are created. This makes the code safer and easier to understand.
Which one should you use?
Always prefer const by default. If you know the value will change, use let. Avoid var in modern JavaScript code.
What is Scope?
Scope simply means: where in your code can a variable be accessed?
Think of it like rooms in a house. If you keep something in your bedroom, only you can access it from your bedroom. People in the living room cannot see what is in your bedroom.
In JavaScript, a block is defined by curly braces { }. Variables declared with let and const inside a block can only be used inside that block.
{
let message = "Hello!";
console.log(message); // works fine here
}
console.log(message); // Error! message does not exist outside the block
But var does not follow block rules. It leaks outside the block, which can cause confusing bugs.
{
var greeting = "Hi!";
}
console.log(greeting); // works, even outside the block
This is why let and const are safer, they stay inside their room and do not cause surprises.
Assignment — Practice it Yourself
Now it is your turn to practice what you have learned.
Task 1 — Declare the following variables and print them in the console:
let name = "Your Name";
let age = 20;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Task 2 — Try changing values for let and const:
let age = 20;
age = 21;
console.log(age); // 21 — works fine
const country = "India";
country = "USA"; // Try this and see what error you get
Observe what happens when you try to change a const value. This error is intentional, it is JavaScript protecting your fixed values.
Conclusion
Variables are the building blocks of every JavaScript program. They store information, make code readable, and help you manage data easily. Using let and const instead of var makes your code cleaner and safer. Understanding data types helps you know what kind of information you are working with. And understanding scope helps you know where your variables live inside your code.
Now every time you see a variable in JavaScript, you know exactly what is happening behind the scenes.
Next, we will explore operators and expressions in JavaScript, how to perform calculations and make decisions using the values stored in your variables.






