Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
7 min read
Understanding Variables and Data Types in JavaScript

When I started learning JavaScript, one of the first things I came across was variables and data types. These concepts look small in the beginning, but they are very important because almost every JavaScript program uses them.

In this article, we’ll understand variables and data types in a simple way with clear examples.

What Is a Variable?

I like to think of a variable as a box.

We can put some information inside that box, give it a name, and use that information later whenever we need it.

For example:

  • A box named name can store "Nawazish"

  • A box named age can store 21

  • A box named isStudent can store true

In JavaScript, variables help us store data so we can use it later in our program.

Why Do We Need Variables?

Imagine writing a program without variables. Every time we needed a value, we would have to type it again and again. That would become messy very quickly.

Variables help us:

  • store values

  • reuse values

  • update values when needed

  • make code easier to read

How to Declare Variables in JavaScript

In JavaScript, we can declare variables using:

  • var

  • let

  • const

1. Using var

var name = "Nawazish"; console.log(name);

var is the older way of declaring variables in JavaScript. It still works, but in modern JavaScript, we usually prefer let and const.

2. Using let

let age = 21; console.log(age);

We use let when the value of a variable may change later.

let city = "Delhi"; city = "Mumbai"; console.log(city); // Mumbai

Here, we changed the value of city, and that is completely allowed with let.

3. Using const

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

We use const when the value should stay fixed and should not be changed later.

const birthYear = 2004; birthYear = 2005; // Error

This gives an error because const variables cannot be reassigned.

Basic Difference Between var, let, and const

Here is the simplest way to understand them:

Keyword Can change value? Modern usage
var Yes Less preferred
let Yes Common
const No Very common

A simple rule I follow as a beginner is:

  • Use const by default

  • Use let if the value will change

  • Avoid var in the beginning

What Are Data Types?

The value stored inside a variable can be of different kinds. These kinds are called data types.

For example:

  • a name is text

  • an age is a number

  • a true/false value is a boolean

JavaScript has many data types, but when I was learning, these primitive data types were the most important to understand first:

  • string

  • number

  • boolean

  • null

  • undefined

1. String

A string is used to store text.

let name = "Nawazish"; let city = "Lucknow";

Strings are usually written inside quotes.

Examples of strings:

  • "Hello"

  • "JavaScript"

  • "My name is Nawazish"

2. Number

A number is used to store numeric values.

let age = 21; let price = 99.99;

In JavaScript, both whole numbers and decimal numbers come under the number type.

3. Boolean

A boolean has only two values:

  • true

  • false

It is mostly used for yes/no or true/false situations.

let isStudent = true; let hasJob = false;

4. Null

null means that the value is intentionally empty.

let middleName = null;

This means we are saying, "Right now, this variable has no value."

5. Undefined

undefined means a variable has been declared, but no value has been assigned yet.

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

Here, score exists, but it does not contain any value yet.

Simple Examples of Data Types

let name = "Aman"; // string let age = 20; // number let isStudent = true; // boolean let address = null; // null let phoneNumber; // undefined

This is how we can store different types of values in variables.

What Is Scope?

When I first heard the word scope, it sounded difficult. But the idea is actually simple.

Scope means where a variable can be used.

Think of it like access to a room.

If something is inside one room, only the people inside that room can use it. People outside cannot access it.

In JavaScript, variables declared inside a block of code are usually available only inside that block.

{ let message = "Hello"; console.log(message); // works here } console.log(message); // Error

Why does the last line give an error?

Because message was created inside the block, so it only exists there.

This is called block scope.

Both let and const follow block scope, which makes them safer and easier to understand.

Scope Visualization Diagram

Here is a simple way to visualize it:

Global Scope | |-- name = "Nawazish" |-- age = 21 | |-- Block Scope |-- let message = "Hello" |-- const isStudent = true

What this means:

  • Variables in the global scope can be used more widely

  • Variables inside a block scope stay inside that block

  • let and const do not escape the block where they are created

A Quick Note on var and Scope

var behaves differently from let and const.

{ var language = "JavaScript"; } console.log(language); // works

Even though language was declared inside a block, it can still be used outside. This is one reason why var can create confusion for beginners.

That is why we usually prefer let and const in modern JavaScript.

Practical Example

Let’s create a few variables for a student profile:

let name = "Nawazish"; let age = 21; let isStudent = true; console.log(name); console.log(age); console.log(isStudent);

Output:

Nawazish 21 true

Now let’s try changing values:

let age = 21; age = 22; console.log(age); // 22

This works because let allows reassignment.

Now with const:

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

This does not work because const does not allow reassignment.

Easy Way to Remember

You can remember it like this:

  • let -> value can change

  • const -> value should stay the same

  • var -> old way, better to avoid as a beginner

Practice Task

Try this on your own:

let name = "Nawazish"; let age = 21; let isStudent = true; console.log(name); console.log(age); console.log(isStudent);

Then do these two experiments:

  • Change the value of age

  • Declare a const variable and try changing it

This will help us understand the difference between let and const in a practical way.

Final Thoughts

Variables and data types are the building blocks of JavaScript. Once I started understanding how to store values and what kind of values I was working with, writing code became much easier.

If you are a beginner, don’t try to master everything at once. Just focus on these basics:

  • what a variable is

  • how to declare it

  • what kind of data it stores

  • where it can be used

Once these concepts are clear, the rest of JavaScript starts making much more sense.