Understanding Variables and Data Types in JavaScript
When we write programs, we need a way to store information so that we can use it later. In JavaScript, this is done using variables and data types.
In this article, we will understand what variables are, how to create them, and the basic data types used in JavaScript.
What Are Variables?
A variable is like a box that stores information.
Imagine you have labeled boxes:
π¦ Name
π¦ Age
π¦ City
Each box stores a specific value.
Programming works the same way. A variable stores a value that we can use later in our program.
Example in JavaScript:
let name = "Parth";
Here:
letβ used to declare a variablenameβ variable name"Parth"β value stored inside the variable
Why Do We Need Variables?
Variables help us:
Store data
Reuse values
Update information easily
Make programs dynamic
Example:
let score = 50;
score = 70;
console.log(score);
Output:
70
Here the value changed from 50 β 70.
Declaring Variables in JavaScript:
In JavaScript, variables can be declared using:
varletconst
Using var
var city = "Delhi";
console.log(city);
var was used in older JavaScript versions.
Using let
let age = 20;
age = 21;
console.log(age);
let allows us to change the value later.
Using const
const country = "India";
console.log(country);
const means the value cannot be changed later.
Example:
const pi = 3.14;
pi = 3.15; // β Error
Primitive Data Types in JavaScript
JavaScript has different data types to store different kinds of values.
String
Used to store text.
Example:
let name = "Parth";
Examples of strings:
"Hello""JavaScript""Parth"
Number
Used to store numbers.
Example:
let age = 20;
let price = 99.99;
JavaScript uses the same type for integers and decimals.
Boolean
Used for true or false values.
Example:
let isStudent = true;
Booleans are commonly used in conditions.
Null
null means intentionally empty value.
Example:
let result = null;
It means the variable exists but currently has no value.
Undefined
undefined means the variable is declared but not assigned a value.
Example:
let marks;
console.log(marks);
Output:
undefined
Difference Between var, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Can redeclare | Yes | No | No |
| Can update value | Yes | Yes | No |
| Scope | Function | Block | Block |
| Recommended today | β No | β Yes | β Yes |
Modern JavaScript usually prefers let and const.
What is Scope? (Beginner Friendly)
Scope means where a variable can be accessed in the code.
Example:
{
let message = "Hello";
console.log(message);
}
This works because we are using the variable inside the block.
But this will give an error:
{
let message = "Hello";
}
console.log(message); // β Error
Because message exists only inside the block.
This is called block scope.
Small Assignment Example
Letβs create some variables and print them.
let name = "Parth";
let age = 20;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output:
Parth
20
true
Now try changing values:
let age = 20;
age = 21; // works
const country = "India";
country = "USA"; // β Error
This shows that const values cannot be changed.
Conclusion
Variables are an essential part of programming because they allow us to store and manipulate data. JavaScript provides three ways to declare variables: var, let, and const.
Understanding variables and data types is the first step toward mastering JavaScript, and they are used in almost every program we write.
As you continue learning, you will use variables to build dynamic and interactive applications.