Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript!!

Updated
β€’4 min readβ€’View as Markdown
Understanding Variables and Data Types in JavaScript!!

🧾 What Are Variables and Why Are They Needed?

A variable is a container used to store data.

We need variables because:

  • To store user input

  • To store numbers (like age, marks, price)

  • To store text (like name, city)

  • To reuse values later in the program

Without variables, we cannot build real applications.

✍️ How to Declare Variables in JavaScript

In JavaScript, we declare variables using:

  • var

  • let

  • const


πŸ”Ή Using var (Old Way)

var name = "Bhavesh";

πŸ”Ή Using let (Modern Way)

let age = 20;

You can change its value later:

age = 21;

πŸ”Ή Using const (Constant Value)

const country = "India";

You cannot change it later:

country = "USA"; // ❌ Error

🧩 What Are Data Types?

A data type tells JavaScript what kind of value we are storing inside the variable.

Let’s understand with simple examples:

Example Data Type
"Bhavesh" String
20 Number
true Boolean

πŸ”Ή Primitive Data Types (Basic Ones)

1️⃣ String (Text)

Used for names, messages, etc.

let name = "Bhavesh";

πŸ‘‰ Text must be inside quotes " " or ' '.


2️⃣ Number

Used for numbers (both integer and decimal).

let age = 20;
let price = 199.99;

3️⃣ Boolean

Used for true/false values.

let isStudent = true;

πŸ‘‰ Mostly used in conditions.


4️⃣ Null

Represents intentional empty value.

let middleName = null;

It means: "I know there is no value right now."


5️⃣ Undefined

When a variable is declared but not given any value.

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

πŸ” Basic Difference Between var, let, and const

Here’s a simple comparison:

Feature var let const
Can Reassign? βœ… Yes βœ… Yes ❌ No
Block Scope? ❌ No βœ… Yes βœ… Yes
Recommended? ❌ Avoid βœ… Yes βœ… Yes

πŸ‘‰ In modern JavaScript, always prefer let and const.


πŸ“Œ What is Scope?

Scope means:

πŸ‘‰ Where your variable can be used.

Think of it like a room πŸšͺ.

If you keep a box inside a room, you can only access it inside that room.

Example:

{
  let age = 20;
  console.log(age); // βœ… Works here
}

console.log(age); // ❌ Error (outside block)

Because let and const follow block scope.

var does not follow block scope, which can sometimes create confusion β€” that’s why we avoid it.


πŸ”„ Showing How Values Can Change

let score = 50;
score = 80; // βœ… Allowed

const total = 100;
total = 200; // ❌ Not allowed

πŸ‘‰ let can change

πŸ‘‰ const cannot change


πŸ§ͺ Assignment (Practice Now!)

Try this in your browser console or VS Code:

1️⃣ Declare these variables:

  • Name

  • Age

  • IsStudent

Example:

let name = "Bhavesh";
let age = 20;
let isStudent = true;

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

2️⃣ Now try this:

  • Change the value of age

  • Try changing the value of a const variable

  • Observe what happens πŸ‘€


πŸ“Š Quick Visual Comparison

var vs let vs const

var    β†’ Old, avoid using
let    β†’ Can change value
const  β†’ Cannot change value

πŸ”Ž Simple Scope Visualization

Global Area
  |
  |-- Block {
         let variable β†’ works only here
     }
1 views