Understanding Variables and Data Types in JavaScript
Topics to Cover
What variables are and why they are needed
How to declare variables using
var, let and constPrimitive data types (string, number, boolean, null, undefined)
Basic difference between
var,let, andconstWhat is scope (very beginner-friendly explanation)
When we begin learning JavaScript, the very first building block we encounter is variables.
Every program needs to store information — names, numbers, user inputs, or even true/false values.
In JavaScript, we store this information using variables.
Let’s understand this clearly and step by step
What is a Variable?
A variable is a named container used to store data in memory.
Imagine you are moving into a new house. You have a lot of items—books, clothes, and kitchenware. To keep things organized, you put them into boxes and put a label on each box.
Think of it like a labeled box:
The label is the variable name.
The box is the variable.
The content inside the box is the value.
JavaScript stores this inside memory while the program runs.
Without variables, we would not be able to store or reuse information in our programs.
How to Declare Variables in JavaScript
JavaScript provides three keywords to declare variables:
var
let
const
Var
var is the older way of declaring variables.
var age = 25;
age = 30; // value can be changed
Key Points:
Value can be reassigned.
Variable can be redeclared.
It does not follow block scope properly.
Because of scope-related issues, var is generally avoided in modern JavaScript.
Let
let is used when the value may change later.
let city = "Delhi";
city = "Bangalore"; // allowed
Key Points:
Value can be reassigned.
Cannot be redeclared in the same scope.
Follows block scope.
Most modern JavaScript code prefers let.
Const
const is used when the value should not change(like your birth date). Short for "constant."
Key Points:
Cannot be reassigned.
Cannot be redeclared.
Follows block scope.
Best practice:
Use const by default. Use let only when the value needs to change.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Reassignment | yes | yes | no |
| Redeclaration | yes | no | no |
Types of Data in JavaScript
In JavaScript, data types are broadly divided into two categories:
Primitive Data Types
Non-Primitive Data Types
JavaScript supports different kinds of data. In this article, we will focus on the most basic and commonly used types.
Primitive Data Types
Variables can store different kinds of values. These basic types are known as primitive data types.
1. String(Text)
Anything inside quotes ("" or '') is a String.
Real-world use: Storing a username, a street address, or a tweet.
Important Characteristics
Strings are primitive and immutable — once created, their content cannot be changed.
Methods like
toUpperCase()return a new string instead of modifying the original.JavaScript does not have a separate character type; a single character is simply a string of length 1.
Strings are indexable (read-only).
2. Number
The number type represents both integers and decimal values.
let age = 25;
let price = 99.99;
JavaScript does not differentiate between integers and floating-point numbers — both are simply numbers.
3. Boolean.
A boolean represents logical values: True or False (Think of it like a light switch-only two states).It is mostly used in decision-making.
let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn); // true
console.log(hasPermission); // false
let age = 20;
let isAdult = age >= 18;
console.log(isAdult); // true
Boolean values are often the result of comparisons.
4. Undefined.
A variable has been declared, but no value has been assigned yet. When we declare a variable without initializing it, JavaScript automatically stores undefined in it.
( Imagine reserving a box but not putting anything inside it yet. The box exists but it is empty.)
let score;
console.log(score); // undefined
- Memory is created for score. Since no value is assigned, JavaScript sets it to. Undefined is real value.
5. Null.
The variable intentionally has no value.
Unlike undefined, null is explicitly assigned by the developer.
let selectedUser = null;
console.log(selectedUser); // null
console.log(typeof null); // "object"
- Memory is created for selectedUser but We deliberately store null inside it. It represents an intentional absence of value.
Understanding Scope
+----------------------------------+
| Global Scope |
| |
| +--------------------------+ |
| | Block Scope | |
| | | |
| | let / const → inside | |
| +--------------------------+ |
| |
| var → accessible outside block |
+----------------------------------+
After declaring variables, an important concept to understand is scope. It determines where a variable can be accessed in the program.
Not every variable is accessible everywhere.
Global Scope
If a variable is declared outside any block or function, it becomes globally accessible.
greeting is declared outside the function. So it can be used anywhere in the program.
Block Scope
If a variable is declared inside { },it can only be used inside that block.
{
let age = 25;
console.log(age); // 25
}
console.log(age); // Error
age exists only inside the block Outside the block, it does not exist
Important Note
let and const follow block scope.
var does not properly respect block boundaries.
That’s why in modern JavaScript, we prefer let and const.
Conclusion
In this article, we explored one of the most fundamental concepts in JavaScript — variables.
We learned how to declare variables using let, const and var, and understood when to use each of them. We also explored primitive data types like string , number, boolean , undefined and null along with how they behave in memory.
Understanding variables is not just about syntax. It’s about knowing:
How data is stored
How values can change
Where variables can be accessed (scope)
And how JavaScript handles uninitialized or empty values
These concepts may seem simple at first, but they form the backbone of every JavaScript program — whether it's a small script or a large application.