Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Published
5 min read

When you start programming, one of the first things you learn is how to store information.

In JavaScript, we store information using variables.

Before jumping into syntax, let’s understand this with a simple real-life analogy.

A Simple Analogy: Variables as Boxes

Imagine you have several boxes at home.

Each box can store something different:

  • One box stores your name

  • One box stores your age

  • One box stores whether you are a student

To identify them easily, you put labels on each box.

Programming works in a very similar way.

A variable is like a labeled box that stores information.

Name Box   → "Sayantan"
Age Box    → 21
Student Box → true

In JavaScript, we give these boxes names so we can access the values later.

What Are Variables?

A variable is a container used to store data that can be used later in a program.

For example:

let name = "Sayantan";
let age = 21;
let isStudent = true;

Here:

  • name stores text

  • age stores a number

  • isStudent stores a boolean value (true/false)

We can print them using:

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

Output:

Sayantan
21
true

Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  • var

  • let

  • const

1️⃣ Using var

var was the original way to declare variables in JavaScript.

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

You can also change the value later.

var city = "Kolkata";
city = "Delhi";

console.log(city);

Output:

Delhi

However, var is less commonly used today because it has some confusing behaviors.

2️⃣ Using let

let is the modern way to declare variables that may change later.

let score = 50;
score = 80;
console.log(score);

Output:

80

Use let when the value may change in the future.

3️⃣ Using const

const is used when the value should not change after being assigned.

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

If we try to change it:

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

JavaScript will throw an error.

TypeError: Assignment to constant variable

Use const when the value should stay fixed.

Primitive Data Types in JavaScript

A data type tells JavaScript what kind of value is stored inside a variable.

JavaScript has several primitive data types.

For beginners, these are the most important ones:

  • String

  • Number

  • Boolean

  • Null

  • Undefined

NO! NOT OBJECT❌ I made mistake multiple times

String

A string represents text.

Example:

let name = "Sayantan";

Examples of strings:

"Hello"
"JavaScript"
"My name is Sayantan"

Number

A number represents numeric values.

Example:

let age = 21;
let price = 99.99;

Numbers can be:

  • Integers → 10

  • Decimals → 10.5

Boolean

A boolean has only two possible values:

true
false

Example:

let isStudent = true;
let isLoggedIn = false;

Booleans are often used in conditions and decision making.

Null

null means intentional absence of a value.

Example:

let selectedUser = null;

This means the variable exists but currently has no value.

Undefined

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

Example:

let marks;
console.log(marks);

Output:

undefined

Difference Between var, let, and const

In modern JavaScript:

  • Use const by default

  • Use let when values need to change

  • Avoid var in most cases

What is Scope? (Beginner Explanation)

Scope determines where a variable can be accessed in the code.

Think of scope like rooms in a house.

If you keep an object inside one room, it may not be visible from another room.

Example:

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

console.log(message);

Output:

Hello
ReferenceError

Why?

Because message was created inside the block { }.

So it cannot be accessed outside it.

This is called block scope.

Practical Example

Let’s store some real information in variables.

let name = "Sayantan";
let age = 21;
let isStudent = true;

console.log("Name:", name);
console.log("Age:", age);
console.log("Is Student:", isStudent);

Output:

Name: Sayantan
Age: 21
Is Student: true

Small Assignment (Practice)

Try writing this code yourself.

Step 1: Declare variables

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

Step 2: Print them

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

Step 3: Try changing values

age = 25;
console.log(age);

Step 4: Try modifying a const variable

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

Observe the error JavaScript gives.

Scope Visualisation Diagram

Variables inside the block scope cannot be accessed outside.

1 views