Sitemap

Understanding Variables and Data Types in JavaScript

5 min readMar 16, 2026

--

A beginner-friendly guide with simple examples anyone can understand

Imagine a Room Full of Boxes 📦

Think about your room for a moment.

You probably have boxes, drawers, or folders where you keep different things — clothes in one drawer, books on a shelf, important documents in a folder.

Each place has a label and a purpose.

Programming works in a very similar way.

When a computer program needs to remember something — like a person’s name, age, or whether they are logged in — it stores that information in a variable.

A variable is simply a labeled box that stores information.

Let’s explore how this idea works in JavaScript, one of the most popular programming languages used to build websites and web applications.

What Are Variables and Why Do We Need Them?

Imagine writing a program that prints a person’s name:

Raghav

Now imagine the name changes to Aman.

Without variables, you would need to change the name everywhere in the program.

Variables solve this problem.

A variable stores information so we can reuse or update it easily.

Example in JavaScript:

let name = "Raghav";

Here:

  • name → the label on the box
  • "Raghav" → the value stored inside the box

Now whenever the program needs the name, it simply uses:

console.log(name);

Output:

Raghav

This simple idea allows programs to become flexible and dynamic.

Declaring Variables in JavaScript

JavaScript gives us three ways to create variables:

  • var
  • let
  • const

Let’s understand them in the simplest way possible.

1. Using var

var is the old way of creating variables in JavaScript.

Example:

var city = "Delhi";
console.log(city);

Years ago, developers used var everywhere.

Today, most developers prefer let and const because they behave more predictably.

2. Using let

let is used when the value might change later.

Think about something like a game score or bank balance. These values can change.

Example:

let score = 10;
score = 20;
console.log(score);

Output:

20

Here the value changed, and JavaScript allowed it.

3. Using const

const stands for constant.

A constant is something that should not change once it is set.

Example:

const country = "India";
console.log(country);

If you try to change it:

country = "USA";

JavaScript will show an error.

This is useful for values that should always remain the same, such as:

  • country name
  • fixed configuration values
  • mathematical constants

Understanding Data Types (In Very Simple Terms)

The type of information stored in a variable is called a data type.

Just like different boxes in your home contain different things — clothes, books, tools — variables also store different types of data.

Let’s look at the most common ones.

1. String (Text)

A string represents text.

Example:

let name = "Raghav";
let city = "Modinagar";

Anything inside quotes (" " or ' ') is a string.

Examples from daily life:

  • names
  • addresses
  • messages
  • email IDs

2. Number

Numbers represent numeric values.

Get Raghav Vasdeva’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

let age = 25;
let price = 199.99;

Numbers are used for:

  • age
  • price
  • marks
  • quantity

3. Boolean (True or False)

A boolean value can only be:

  • true
  • false

Example:

let isStudent = true;
let isLoggedIn = false;

These are often used when programs make decisions.

Example:

  • Is user logged in?
  • Did payment succeed?
  • Is form valid?

4. Null

null means the value is intentionally empty.

Example:

let middleName = null;

This means the variable exists, but it currently has no value.

5. Undefined

undefined means a variable was created but no value was assigned yet.

Example:

let score;
console.log(score);

Output:

undefined

It’s like creating an empty box but not putting anything inside it yet.

Simple Difference Between var, let, and const

Here is a quick and beginner-friendly comparison.

| Feature          | var      | let    | const       |
| ---------------- | -------- | ------ | ----------- |
| Can change value | Yes | Yes | No |
| Can redeclare | Yes | No | No |
| Scope | Function | Block | Block |
| Modern usage | Rare | Common | Very common |
Press enter or click to view image in full size

Most developers follow a simple rule:

👉 Use const by default
👉 Use let when values will change
👉 Avoid var

What is Scope? (Explained Like a Room)

Scope simply means:

Where a variable can be used in a program.

Imagine keeping a book inside your room.

People inside the room can access it.

People outside the room cannot.

In JavaScript, code blocks behave like rooms.

Example:

{
let message = "Hello";
console.log(message);
}

console.log(message);

The second line will produce an error.

Why?

Because the variable exists only inside that block { }.

Simple Visualization

House
│
├── Room 1
│ let a = 10
│
└── Room 2
const b = 20

Variables inside one room cannot be accessed outside it.

This keeps programs organized and safe.

Press enter or click to view image in full size

A Small Example Program

Let’s combine everything we learned.

let name = "Raghav";
let age = 25;
let isStudent = true;

console.log(name);
console.log(age);
console.log(isStudent);

Output:

Raghav
25
true

Try It Yourself (Small Assignment)

Open your browser console and try this.

Step 1: Create Variables

let name = "Your Name";
let age = 22;
let isStudent = true;

Step 2: Print Them

console.log(name);
console.log(age);
console.log(isStudent);

Step 3: Experiment

Try changing a value:

age = 23;

Now try the same with const.

const country = "India";
country = "USA";

Observe what happens.

Curiosity and experimentation are the best teachers in programming.

Final Thoughts

Variables are one of the first building blocks of programming.

They allow programs to:

  • store information
  • update values
  • make decisions
  • create dynamic experiences

In JavaScript:

  • let is used when values may change
  • const is used when values should stay fixed
  • data types define what kind of value a variable holds

Once you understand variables and data types, you have taken your first real step into programming.

And just like those labeled boxes in a room, every well-written program begins with organizing information in the right place.

✨ If you’re starting your JavaScript journey, mastering these basics will make the rest of the language much easier to understand.

--

--