Understanding Variables and Data Types in JavaScript

Every program needs a way to store information.
Imagine writing a program that tracks a user’s name, age, or whether they are logged in. The program needs a place to keep that information so it can use it later.
In JavaScript, we store information using variables.
Before we dive into syntax, let’s start with a simple way to think about it.
Think of Variables Like Boxes
Imagine you have small labeled boxes on your desk.
Each box can hold something:
One box holds your name
Another box holds your age
Another box stores whether you are a student
The label on the box is the variable name, and the value inside the box is the data stored in it.
In JavaScript, it looks like this:
let name = "Alex";
let age = 21;
Here:
nameis the box label"Alex"is the value stored in it
The program can use this information whenever it needs it.
Declaring Variables in JavaScript
JavaScript provides three main ways to create variables:
varletconst
These keywords tell JavaScript that we want to declare a variable.
Using let
let is the most commonly used way to declare variables today.
let city = "London";
You can change the value later if needed.
let city = "London";
city = "Paris";
The value inside the box changes.
Using const
const is used when the value should not change.
const country = "India";
If you try to change it:
country = "USA";
JavaScript will produce an error because constants are meant to stay the same.
Using var
var is the older way to declare variables in JavaScript.
var score = 100;
It still works, but modern JavaScript usually prefers let and const because they behave more predictably.
Primitive Data Types in JavaScript
The value inside a variable can have different types of data.
JavaScript has several basic data types called primitive data types.
Let’s look at the most common ones.
String
A string represents text.
Examples include names, messages, or any sequence of characters.
let name = "Sarah";
Anything inside quotes (" " or ' ') is treated as a string.
Examples:
let city = "Tokyo";
let greeting = "Hello world";
Number
Numbers represent numeric values.
let age = 25;
let price = 19.99;
JavaScript uses the same type for both integers and decimals.
Boolean
A boolean represents either true or false.
This is often used for conditions.
let isLoggedIn = true;
let isStudent = false;
Booleans are very common in decision-making logic.
Null
null represents an intentional empty value.
It means the variable exists, but currently holds nothing.
let selectedUser = null;
You might use this when waiting for a value to be assigned later.
Undefined
undefined means a variable has been declared but no value has been assigned yet.
Example:
let score;
If you print score, it will show undefined.
Basic Difference Between var, let, and const
Here is a simple way to understand the differences.
| Keyword | Can Change Value | Modern Usage |
|---|---|---|
| var | Yes | Older JavaScript |
| let | Yes | Recommended |
| const | No | Recommended for fixed values |
Example:
let age = 20;
age = 21; // allowed
const birthYear = 2002;
birthYear = 2003; // error
As a general rule:
Use
constwhen the value should stay the sameUse
letwhen the value may change
What is Scope?
Scope determines where a variable can be used in your program.
Think of scope as the area where the box is visible.
If a variable is declared inside a block of code, it may only be accessible inside that block.
Example:
{
let message = "Hello";
console.log(message);
}
Inside the block, the variable works normally.
But outside the block:
console.log(message);
JavaScript will produce an error because the variable is out of scope.
For beginners, the key idea is simple:
Scope controls where variables can be accessed.
A Practical Example
Here is a small example that uses multiple data types.
let username = "David";
let age = 22;
let isMember = true;
console.log(username);
console.log(age);
console.log(isMember);
Output:
David
22
true
Each variable stores a different kind of information.
Assignment
Try the following exercise in your browser console or a JavaScript file.
Step 1: Declare Variables
Create variables for:
Name
Age
IsStudent
Example structure:
let name = "John";
let age = 20;
let isStudent = true;
Step 2: Print Them
Use console.log() to display them.
console.log(name);
console.log(age);
console.log(isStudent);
Step 3: Experiment with let and const
Try changing the values.
Example:
let age = 20;
age = 21;
This will work.
Now try this:
const name = "John";
name = "Mike";
You will get an error because constants cannot be reassigned.
Observe how JavaScript behaves when you try both.
Final Thoughts
Variables are one of the most fundamental parts of programming. They allow programs to store and manipulate information, which is necessary for almost everything—from simple calculations to full web applications.
Understanding how variables work, how different data types behave, and when to use let or const builds a strong foundation for learning JavaScript.
Once these basics become familiar, it becomes much easier to move on to more advanced concepts like functions, objects, and asynchronous programming.






