Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
•5 min read•View as Markdown
Understanding Variables and Data Types in JavaScript
S

Hi, I’m Swastisunder 👋 I’m a full-stack web developer focused on building clean, modern, and practical web applications. I work mainly with the MERN stack (MongoDB, Express, React, Node.js) and enjoy turning ideas into real, usable products. Right now, I’m also learning Python and AI/ML, one concept and one project at a time. Always learning. Always building.

Imagine you have few boxes on your table.

One box store your name.

One box store your age.

One box store your marks.

Whenever you need any information, you simply open the correct box.

Variables work exactly like these boxes.

Variables store data so we can use it later in our program.

Without variables, we have to write same values again and again.

In this article we will understand:

  • What variables are

  • Why we need them

  • var, let and const

  • Primitive data types

  • Scope

  • Small assignment for practice


What is a Variable?

A variable is a container which store data.

Example:

let name = "Swasti";

Here,

  • name is variable.

  • "Swasti" is value stored inside it.

Whenever we need the value, we can use variable instead of writing it again.

Example:

console.log(name);

Output:

Swasti

Why Variables are Needed?

Imagine writing your name 20 times in your code.

Instead of doing that, you store it once.

Example:

let name = "Swasti";

console.log(name);
console.log(name);
console.log(name);

If later your name changes, you only change one line.

Variables make code easier to read and manage.


Declaring Variables

In JavaScript we can declare variables using:

  • var

  • let

  • const

Let's understand them one by one.


var

var was used in older JavaScript.

Example:

var city = "Surat";

console.log(city);

You can change its value.

var city = "Surat";

city = "Ahmedabad";

console.log(city);

Output:

Ahmedabad

Nowadays people mostly use let and const.


let

let is used when value can change later.

Example:

let score = 10;

score = 20;

console.log(score);

Output:

20

Whenever you know value may change, use let.


const

const is used when value should not change.

Example:

const country = "India";

console.log(country);

If you try changing it,

const country = "India";

country = "USA";

JavaScript will throw an error.

Because const variables cannot be reassigned.


Primitive Data Types

JavaScript have different types of data.

Let's see the common ones.


String

String store text.

Example:

let name = "Swasti";

Strings are written inside quotes.


Number

Number store numeric values.

Example:

let age = 22;

Both integers and decimal numbers are number type.

Example:

let price = 199.99;

Boolean

Boolean have only two values.

  • true

  • false

Example:

let isStudent = true;

This type is mostly used for conditions.


Null

null means value is intentionally empty.

Example:

let user = null;

Maybe user information will come later.

Till then it is null.


Undefined

Undefined means value is not assigned yet.

Example:

let city;

console.log(city);

Output:

undefined

Variable exist, but no value is stored inside it.


Difference Between var let and const

Keyword Can Change Value Mostly Used Today
var Yes No
let Yes Yes
const No Yes

Simple rule:

  • Use const by default.

  • Use let if value will change.

  • Avoid using var in new code.


What is Scope?

Scope simply means,

"Where can we use a variable?"

Example:

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

This works.

But,

{
    let message = "Hello";
}

console.log(message);

This gives error.

Because message was created inside the block.

Outside the block it does not exist.

Think of scope like a room.

If your bag is inside your room, people outside cannot access it.

Same idea applies to variables.


Small Practice Assignment

Create variables for:

  • Name

  • Age

  • IsStudent

Example:

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

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

Now try this:

Change value of age.

age = 23;

It will work.

Now try changing a const variable.

const country = "India";

country = "USA";

See what happens.

Learning by trying is always better than only reading.


One Important Thing

Many people try to memorize everything.

You don't need that.

Just remember:

  • Variables store values.

  • let can change.

  • const cannot change.

  • Different data types store different kinds of data.

The more you practice, the easier these concepts become.


Conclusion

Variables are one of the most important concepts in JavaScript.

Every JavaScript program uses variables to store and manage data.

In this article we learned:

  • What variables are

  • Why variables are needed

  • var, let, and const

  • Primitive data types

  • Basic scope

  • Small practice assignment

Once these concepts are clear, learning functions, loops, and objects becomes much easier because almost every JavaScript program starts with variables.

More from this blog

W

WebDev 2026

16 posts

A developer blog where I share web development, networking, JavaScript, Git, Node.js and computer science concepts in simple and beginner friendly way. Some blogs are technical deep dives, some are story based explanations, and some are part of my learning journey as a developer. This blog is basically my public notebook where I learn, build, experiment and explain things in the simplest way possible.