Understanding Variables and Data Types in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators. If you’ve ever done basic ma

When learning JavaScript, you’ll eventually encounter a keyword that can feel confusing at first: this. The value of this changes depending on how a function is called. JavaScript also provides specia

Functions are one of the most important concepts in JavaScript. They help you organize your code and avoid repeating the same logic again and again. In JavaScript, there are multiple ways to create fu

When writing JavaScript programs, you often need to store multiple values together. For example, imagine you want to store a list of fruits: Apple Mango Banana Orange You could store them like t

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programmi

ygshjm
24 posts
When starting with JavaScript, one of the first things you learn is variables and data types. These are fundamental concepts that help you store and manage information in your programs.
Think of variables as containers that hold information, and data types as the kind of information inside those containers.
Imagine you have a box where you store something important — maybe your phone, keys, or wallet. Instead of holding them in your hand all the time, you put them in a box so you can use them later.
In programming, variables work like that box.
They store information so your program can use it later, update it, or display it.
For example, a website might need to store:
A user's name
Their age
Whether they are logged in
In JavaScript, we store those values using variables.
let name = "Rahul";
let age = 25;
let isLoggedIn = true;
Your program can now reuse these values anywhere they are needed.
In JavaScript, there are three ways to create variables:
var
let
const
Each one declares a variable, but they behave slightly differently.
varvar city = "delhi";
var was the original way to create variables in JavaScript.
However, modern JavaScript usually prefers let and const because they behave more predictably.
letlet age = 21;
let creates a variable whose value can change later.
Example:
let score = 10;
score = 20;
console.log(score); // 20
Here we changed the value from 10 to 20.
constconst country = "India";
const means the value cannot be reassigned later.
Example:
const pi = 3.14;
pi = 3.14159; // This will cause an error
Because const variables are meant to stay constant.
Every variable stores some kind of data. The type of data stored is called a data type.
JavaScript has several primitive data types, but beginners usually start with these five:
String
Number
Boolean
Null
Undefined
A string represents text.
Examples include:
names
messages
addresses
Example:
let name = "Aman";
let message = "Welcome to my website";
Strings are written inside quotes.
A number represents numeric values.
Example:
let age = 25;
let price = 199.99;
Numbers can be:
whole numbers (10)
decimal numbers (3.14)
A boolean represents only two possible values:
true
false
Example:
let isLoggedIn = true;
let hasPermission = false;
Booleans are often used for conditions and decisions.
null represents an intentional empty value.
It means the variable exists but currently has no value assigned.
Example:
let selectedUser = null;
You might assign a value later.
undefined means a variable has been declared but not given a value yet.
Example:
let username;
console.log(username); // undefined
The variable exists, but nothing has been stored inside it yet.
| Keyword | Can Change Value | Modern Usage |
|---|---|---|
| var | Yes | mostly avoided |
| let | yes | commonly used |
| const | No (cannot reassign) | Very common |
example:
var a = 10;
let b = 20;
const c = 30;
a = 15; // allowed
b = 25; // allowed
c = 35; // error
Scope determines where a variable can be used in your code.
Think of scope like rooms in a house.
If you keep something in your bedroom, people in the living room cannot access it.
Similarly, variables defined in one place may not be accessible everywhere.
function greet() {
let message = "Hello!";
console.log(message);
}
greet();
Here:
message exists inside the function
It cannot be used outside that function
Scope helps keep code organized and prevents conflicts between variables.