Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Learn what variables are, how to use var, let, and const, and understand JavaScript primitive data types with simple examples.

Updated
3 min readView as Markdown
Understanding Variables and Data Types in JavaScript
S

Hi, I’m Shubham 👋 A learner sharing my journey in programming, tech, and self-growth. Learning every day and writing what I learn.

When you start learning JavaScript, the first thing you meet is variables and data types. These are the building blocks of programming.

Let’s understand them in the simplest way possible.


Real-Life Analogy: Variables Are Like Boxes

Imagine you have different boxes at home.

  • One box stores your name

  • One box stores your age

  • One box stores whether you are a student

Each box has:

  • A label (variable name)

  • A value inside it (data)

In JavaScript, variables work exactly like that.


What Are Variables?

A variable is a container (box) used to store data.

We use variables because:

  • We need to store information.

  • We want to reuse data.

  • We want to update values later.

Example:

let name = "Shubham";

Here:

  • name → box label

  • "Shubham" → value inside the box


How to Declare Variables

In JavaScript, we can create variables using:

  • var

  • let

  • const

Using var

var city = "Patna";

Using let

let age = 22;

Using const

const country = "India";

Primitive Data Types

Data types tell us what kind of data we are storing.

String (Text)

Used for names, messages, etc.

let name = "Shubham";

👉 Text must be inside quotes " " or ' '.


Number

Used for age, marks, price, etc.

let age = 22;
let price = 99.99;

Boolean

Used for true/false values.

let isStudent = true;

null

Means intentionally empty.

let middleName = null;

undefined

Means value is not assigned yet.

let marks;
console.log(marks); // undefined

Difference Between var, let, and const (Beginner Level)

Feature var let const
Can change value? ✅ Yes ✅ Yes ❌ No
Can redeclare? ✅ Yes ❌ No ❌ No
Scope Function Block Block

What Is Scope? (Very Simple Explanation)

Scope means:

Where can you access your variable?

Think of scope like room access.

If a variable is inside a room (block), you cannot use it outside that room.

Example:

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

console.log(message); // ❌ Error

Because message only exists inside that block { }.


Changing Values

With let

let age = 22;
age = 23; // ✅ Allowed

With const

const country = "India";
country = "USA"; // ❌ Error

const means constant → value cannot change.


Assignment

Try this:

let name = "Shubham";
let age = 22;
let isStudent = true;

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

Now try:

  • Change age

  • Change isStudent

  • Make one variable const and try changing it

Observe what happens.


Simple Scope Visualization

Global Scope
 ├── name
 ├── age
 └── isStudent

Block Scope
 └── message (only inside block)

Final Thoughts

Variables are just labeled boxes.

Data types tell us what kind of thing is inside the box.

Once you understand variables clearly, the rest of JavaScript becomes much easier.

Start small. Practice daily. Keep experimenting.