Understanding Variables and Data Types in JavaScript
When you begin learning programming, one of the first concepts you encounter is variables. Variables are fundamental because they allow programs to store and work with information.
Whether you are storing a person's name, tracking a score in a game, or saving a user's age, variables make it possible to manage that data inside a program.
In this article, we will explore:
What variables are and why they are needed
How to declare variables using
var,let, andconstPrimitive data types (
string,number,boolean,null,undefined)The basic difference between
var,let, andconstWhat scope means in very simple terms
All examples are simple and easy to try in your browser console or Node.js.
A Simple Analogy: Variables as Boxes
A helpful way to understand variables is to imagine a box that stores information.
Each box has:
A name (the variable name)
A value inside it (the stored data)
For example:
Box name → age
Value inside → 25
In JavaScript, we create that box using a variable.
Example:
let age = 25;
Here:
age→ name of the box25→ value stored in the box
The program can now use this information whenever needed.
Why Variables Are Needed
Variables help programs store and reuse data.
Imagine a program without variables. Every time we needed a value, we would have to write it again and again.
Example without variables:
console.log("Shivam");
console.log("Shivam");
console.log("Shivam");
With variables:
let name = "Shivam";
console.log(name);
console.log(name);
console.log(name);
If the name changes, we only update it once, and the whole program uses the new value.
Declaring Variables in JavaScript
In JavaScript, we can declare variables using three keywords:
varletconst
Example:
var city = "Mumbai";
let age = 22;
const country = "India";
Each keyword creates a variable, but they behave slightly differently.
Primitive Data Types in JavaScript
A data type defines the kind of value stored in a variable.
JavaScript has several primitive data types, but beginners usually start with these five.
1. String
A string represents text.
Example:
let name = "Shivam";
Examples of strings:
"Hello"
"JavaScript"
"Hashnode"
Strings are always written inside quotes.
2. Number
A number represents numeric values.
Example:
let age = 21;
let price = 199;
Numbers can be:
10
45
3.14
JavaScript does not separate integers and decimals into different types.
3. Boolean
A boolean represents a logical value.
It can only be:
true
false
Example:
let isStudent = true;
Booleans are often used in conditions and decision-making.
4. null
null represents an empty value that is intentionally set.
Example:
let selectedUser = null;
This means the variable exists, but currently has no value.
5. undefined
undefined means a variable has been declared but no value has been assigned yet.
Example:
let score;
console.log(score);
Output:
undefined
Basic Difference Between var, let, and const
Although all three create variables, they behave differently.
var
var was the original way to declare variables in JavaScript.
Example:
var name = "Aman";
However, modern JavaScript developers prefer let and const because they are safer and clearer.
let
let is used when a variable's value may change later.
Example:
let age = 20;
age = 21;
Here the value was updated.
const
const is used when the value should not change.
Example:
const country = "India";
Trying to change it will cause an error:
country = "USA";
This helps prevent accidental changes.
Comparison of var, let, and const
| Keyword | Can Change Value? | Modern Usage |
|---|---|---|
| var | Yes | Rarely used now |
| let | Yes | Common |
| const | No | Very common |
In modern JavaScript:
Use const by default
Use let if the value needs to change
What is Scope? (Beginner-Friendly Explanation)
Scope simply means where a variable can be accessed in the code.
Think of it like a room in a house.
If a variable is created inside a room, it can only be used inside that room.
Example:
{
let message = "Hello";
console.log(message);
}
Inside the block → it works.
But outside the block:
console.log(message);
This will cause an error because the variable only exists inside that block.
So scope controls where variables are visible and usable.
Example Showing Value Changes
Using let:
let score = 10;
score = 20;
console.log(score);
Output:
20
Using const:
const score = 10;
score = 20;
This will produce an error because const values cannot be reassigned.
Assignment (Try This Yourself)
Open your browser console or Node.js terminal and try the following.
Step 1: Declare Variables
let name = "Shivam";
let age = 21;
let isStudent = true;
Step 2: Print Them
console.log(name);
console.log(age);
console.log(isStudent);
Step 3: Try Changing a let Variable
let age = 21;
age = 22;
console.log(age);
Observe that the value changes successfully.
Step 4: Try Changing a const Variable
const country = "India";
country = "USA";
Notice that JavaScript throws an error.
This helps you understand how const protects values from accidental changes.
Simple Scope Visualization
Global Area
|
|--- Variable A (accessible everywhere)
Block Scope
|
|--- Variable B (only inside block)
Variables created inside a block cannot be accessed outside it.
Final Thoughts
Variables are one of the most essential building blocks of programming. They allow programs to store information, update values, and reuse data efficiently.
By understanding:
how to declare variables
how different data types work
when to use
var,let, andconstand what scope means
you build a strong foundation for writing JavaScript programs.
The best way to learn is simple: experiment in your console. Change values, create variables, and observe how the program behaves. Over time, working with variables will become second nature in your coding journey.