Understanding Variables and Data Types in JavaScript
JavaScript Data Types

What variables are and why they are needed in programming.
In programming, variables are used to store information so that a program can use it later.
They act like containers that hold data, such as names, numbers, or messages.
Variables are needed because they help us store, reuse, and change values easily in our code.
Variables in JavaScript
A variable is a named container used to store data values that can be accessed and modified later in a program.
JavaScript is a dynamically typed language, which means you don’t need to specify the data type when declaring variables.
A single variable can store different types of values such as numbers, text, or objects.
Example
let name = "Sudhanshu"; //name stores a string
let age = 20; //age stores a number
let isStudent = true; //isStudent stores a boolean
How to declare variables using `var`, `let`, and `const`.
In JavaScript, a variable is a named container used to store data that a program can use later.
Variables are declared using var, let, or const.
1. var
var is used to create a variable that stores a value, and it can be used inside a function or globally in the program.
Imagine a shopping website storing the total price.
var totalPrice = 500;
console.log(totalPrice);
//If the user adds more items:
totalPrice = 800;
console.log(totalPrice);
//Now the value changes from 500 → 800.
i ) Why var Is Not Recommended in Modern JavaScript
var ignores blocks like if, for, or { }. This can create problems.
if (true) {
var city = "Delhi";
}
console.log(city); // ✅ Works
Even though city was created inside the if block, it is still accessible outside.
This can make programs harder to understand.
ii) var Hoisting Can Be Confusing
Variables declared with var are hoisted and initialized as undefined, which can lead to confusing results.
console.log(price); // undefined
var price = 100;
2. let
let was introduced in ES6 to declare variables that are block-scoped. A let variable cannot be redeclared in the same block, but it can be reassigned. It can also be initialized when declared.
Example:
let count = 10;
count = 15; // reassignment allowed
console.log(count); // 15
3. const
const declares block-scoped variables whose value cannot be reassigned using the assignment operator. However, if a const variable holds an object or array, its properties or elements can still be added, updated, or removed.
Example:
const name = "Sudhanshu";
console.log(name); // Sudhanshu
// name = "Rahul" Error: cannot reassign a const variable
Difference Between var, let, and const
Primitive Data Types in JavaScript
1️⃣ String
A String is used to store text or characters.
Anything written inside quotes becomes a string.
You can use:
" "Double quotes' 'Single quotesBackticks
Example
let language = "JavaScript";
let country = 'India';
let greeting = `Hello Developer`;
2️⃣ Number
A Number stores numeric values like integers or decimal numbers.
Example
let temperature = 30;
let productPrice = 499.50;
Numbers are written without quotes.
3️⃣ Boolean
A Boolean stores only two values:
truefalse
It is often used in conditions and logical checks.
Example
let isOnline = true;
let hasCompletedCourse = false;
4️⃣ Null
null represents an empty value that is intentionally assigned.
Example
You reserved a seat, but no one is sitting there yet.
let selectedUser = null;
5️⃣ Undefined
undefined means a variable exists but has no value assigned yet.
Example
let score;
console.log(score); // undefined
What is Scope ?
Scope means the area in your code where a variable can be used or accessed.
If a variable is inside the scope, you can use it.
If it is outside the scope, you cannot access it.
Simple Example:
Here, name is in the global scope, so it can be used inside the function.
let name = "Sudhanshu";
function greet() {
console.log(name); // accessible here
}
greet();
Types of Scope in JavaScript
1️⃣ Global Scope
A variable declared outside any function or block is in the global scope.
It can be accessed anywhere in the program.
let name = "Sudhanshu";
function greet() {
console.log(name); // accessible
}
greet();
console.log(name);
2️⃣ Function Scope
Variables declared inside a function can only be used inside that function.
function showMessage() {
let message = "Hello Developer";
console.log(message);
}
showMessage();
// console.log(message); Error
3️⃣ Block Scope
A block scope is created using { }.
Variables declared with let and const stay inside the block.
{
let age = 22;
console.log(age); // works here
}
// console.log(age); Error
Conclusion
Variables are containers used to store and manage data in a program. In JavaScript, we declare variables using var, let, and const, with let and const preferred in modern JavaScript. We also learned about primitive data types such as String, Number, Boolean, Null, and Undefined, which represent basic kinds of data. Finally, scope defines where a variable can be accessed in the code (global, function, and block scope). Understanding these basics helps build a strong foundation for learning JavaScript and programming.




