Understanding Variables and Data Types in JavaScript

What Variables Are and Why They Are Needed?
When writing a program, we often need a place to store information. This information could be a person’s name, age, score, or whether someone is logged in. In JavaScript, variables are used to store such data.
A simple way to understand a variable is to think of it as a box that stores information. The box has a label so we can find it later, and inside the box we keep the value.
For example, imagine you want to store someone’s name. You create a variable and put the name inside it.
let name = "Ravi";
Here, name is the label of the box, and "Ravi" is the value stored inside it. Later in the program, you can use this variable whenever you need that information.
How to Declare Variables Using var, let, and const?
In JavaScript, variables can be declared using three keywords: var, let, and const.
The most common way today is using let.
let age = 20;
You can also use var, which was the older way to declare variables.
var city = "Mumbai";
The third keyword is const, which is used when the value should not change.
const country = "India";
All three keywords create variables, but they behave slightly differently in terms of scope and whether their values can change.
Primitive Data Types
Variables can store different kinds of data. JavaScript has several basic data types called primitive data types.
String
A string represents text. It is written inside quotes.
let name = "Anita";
This is useful for storing names, messages, or any text.
Number
Numbers represent numeric values.
let age = 25;
JavaScript uses the same type for integers and decimal numbers.
let price = 99.99;
Boolean
A boolean represents either true or false.
let isStudent = true;
Booleans are often used in conditions and decision-making.
Null
null represents an intentional empty value.
let result = null;
This means the variable exists but currently holds no meaningful value.
Undefined
undefined means a variable has been declared but not assigned a value.
let score;
console.log(score);
Since no value was assigned, the output will be undefined.
Basic Difference Between var, let, and const
The main differences between these keywords are related to how their values can change and how they behave in scope.
var was used in older JavaScript and can sometimes behave unpredictably because of its wider scope.
let allows the value to change later in the program.
let age = 20;
age = 21;
This works because let variables can be updated.
const is used for values that should remain constant.
const country = "India";
If you try to change it:
country = "USA";
JavaScript will show an error because const values cannot be reassigned.
What Is Scope?
Scope describes where a variable can be accessed in your code.
Imagine writing something inside a room. Only people inside that room can see it. People outside cannot access it.
In programming, a variable declared inside a block of code is usually only available within that block.
For example:
if (true) {
let message = "Hello";
console.log(message);
}
Inside the block, the variable works normally. But outside the block, it cannot be accessed.
This helps keep variables organized and prevents accidental changes in other parts of the program.
Let’s create a few variables and print them.
let name = "Rahul";
let age = 22;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output in the console:
Rahul
22
true
Now try changing the value of age.
age = 23;
console.log(age);
This works because let allows reassignment.
Now try doing the same with const.
const country = "India";
country = "USA";
JavaScript will throw an error because const variables cannot be reassigned.





