Variables and Data Types in JavaScript
When we write programs, we often need to store information so we can use it later.
For example:
A user’s name
A person’s age
Whether a user is logged in or not
In JavaScript, we store information using variables.
In this article, we will learn:
What variables are and why they are needed
How to declare variables using
var,let, andconstPrimitive data types in JavaScript
The basic difference between
var,let, andconstWhat scope means (explained simply)
All examples are beginner-friendly and easy to try in the browser console or Node.js.
What Are Variables?
Think of a variable like a box that stores information.
You can put something inside the box, and later you can take it out and use it.
Example in real life:
A box labeled "name" might store
"Alex"A box labeled "age" might store
25
In JavaScript, variables work in a similar way.
Example
let name = "Alex";
let age = 25;
Here:
nameis a variable storing"Alex"ageis a variable storing25
We can use these variables later in the program.
console.log(name);
console.log(age);
Output:
Alex
25
Declaring Variables in JavaScript
In JavaScript, we create variables using:
varletconst
These keywords tell JavaScript that we are creating a variable.
Using let
let is the most commonly used way to create variables today.
It allows the value of the variable to change later.
Example:
let age = 20;
console.log(age);
age = 21;
console.log(age);
Output:
20
21
Here the value changed from 20 to 21.
Using const
const stands for constant, meaning the value cannot be changed after it is assigned.
Example:
const country = "India";
console.log(country);
Output:
India
If we try to change it:
country = "USA";
JavaScript will give an error, because const variables cannot be reassigned.
So use const when the value should stay the same.
Using var
var was used in older JavaScript code before let and const were introduced.
Example:
var city = "Delhi";
console.log(city);
Output:
Delhi
Today, most developers prefer let and const instead of var, because they behave more predictably.
Primitive Data Types in JavaScript
Variables can store different types of data.
The most common primitive data types are:
stringnumberbooleannullundefined
Let’s look at each one.
String
A string is used to store text.
Example:
let name = "Alex";
let city = "Kolkata";
Here "Alex" and "Kolkata" are strings.
Strings are usually written inside quotes.
Number
A number is used for numeric values.
Example:
let age = 22;
let price = 199;
These are numbers.
JavaScript uses the same type for integers and decimals.
Example:
let rating = 4.5;
Boolean
A boolean represents true or false values.
Example:
let isLoggedIn = true;
let hasPaid = false;
Booleans are often used in conditions and decision making.
null
null represents an intentional empty value.
Example:
let selectedUser = null;
This means the variable currently has no value, but it may get one later.
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
Here is a simple comparison.
| Keyword | Can change value? | Modern usage |
|---|---|---|
| var | Yes | Rarely used now |
| let | Yes | Commonly used |
| const | No | Very commonly used |
Example:
let age = 20;
age = 21; // allowed
const birthYear = 2002;
birthYear = 2003; // error
General rule developers follow:
Use
constby defaultUse
letwhen the value needs to change
What is Scope? (Simple Explanation)
Scope means where a variable can be accessed in your code.
In simple terms:
Scope defines where a variable is visible or usable.
Example:
let age = 20;
if (age > 18) {
let message = "You are an adult";
console.log(message);
}
Inside the block, message works fine.
But outside:
console.log(message);
This will give an error, because the variable only exists inside the block.
So its scope is limited to that block.
Variables are one of the most fundamental concepts in JavaScript.
They allow us to store and work with information in our programs.
Key things to remember:
Variables store data
let,const, andvarare used to declare variablesData types define the kind of data stored
Scope determines where variables can be accessed
