Ticker

6/recent/ticker-posts

Chapter 3: Variables, Data Types, and Memory – How JavaScript Stores Information: whosgeek

 

Chapter 3: Variables, Data Types, and Memory – How JavaScript Stores Information: whosgeek

Imagine Running a Warehouse

Before learning variables, let's imagine you own a warehouse.

Every day, products arrive:

  • Mobile Phones
  • Laptops
  • Keyboards
  • Monitors

You cannot simply throw everything onto the floor.

You need labels.

For example:

Shelf A → Laptops

Shelf B → Phones

Shelf C → Keyboards

When somebody asks:

"Where is the laptop?"

You immediately know where it is stored.

Variables work exactly the same way.

They help JavaScript store and locate information.


What is a Variable?

A variable is a named container used to store data.

Think of it as a labeled storage box.

Example:

let name = "Aqad";

JavaScript creates:

name

"Aqad"

Now whenever we use:

console.log(name);

JavaScript retrieves the stored value.

Output:

Aqad

Why Do We Need Variables?

Imagine building a login page.

Without variables:

"Aqad"
"aqad@email.com"
"India"

You have values but no way to identify them.

Using variables:

let userName = "Aqad";

let email = "aqad@email.com";

let country = "India";

Now every piece of information has a meaningful label.

This makes code readable and maintainable.


How JavaScript Stores Variables

When JavaScript sees:

let age = 25;

It performs three steps.

Step 1

Create a variable named:

age

Step 2

Reserve memory.

Step 3

Store value:

25

Visualization:

Memory

age

25

This process happens continuously while your application runs.


Three Ways to Create Variables

Modern JavaScript provides:

var
let
const

Many beginners become confused about which one to use.

Let's simplify it.


Understanding var

Older JavaScript developers primarily used:

var name = "Aqad";

For many years, this was the only option.

Example:

var city = "Delhi";

console.log(city);

Output:

Delhi

Although var still works, modern projects rarely use it.

Why?

Because it introduced several problems.

We'll understand those later while studying hoisting and scope.


Understanding let

Introduced in ES6.

Example:

let age = 25;

Later:

age = 26;

This works because let allows reassignment.

Think:

let

=
Value Can Change

Example:

let score = 0;

score = 10;

score = 20;

Perfectly valid.


Understanding const

Sometimes values should never change.

Example:

const country = "India";

Attempting:

country = "USA";

Produces:

Error

Because const cannot be reassigned.

Think:

const

=
Permanent Label

Real Life Example

Imagine a company.

Employee ID:

EMP-1001

Never changes.

Use:

const employeeId =
"EMP-1001";

Employee salary:

50000

Can change.

Use:

let salary = 50000;

This simple rule helps developers decide between let and const.


Which Should You Use?

Professional developers generally follow:

Use const first

Use let when needed

Avoid var

Example:

const company =
"AQAD";

let visitors =
100;

This approach reduces bugs.


Understanding Data Types

Variables store values.

Data types define what kind of value is stored.

Imagine a warehouse again.

Different products require different storage.

Examples:

Milk

Furniture

Electronics

Medicine

Similarly, JavaScript stores different types of data differently.


Primitive Data Types

JavaScript has several primitive data types.


String

Strings represent text.

Example:

let name = "Aqad";

Other examples:

let city = "Bangalore";

let company =
"AQAD";

Strings always appear inside:

" "

or

' '

Number

Numbers represent numeric values.

Example:

let age = 25;

Examples:

let price = 1000;

let temperature = 34.5;

JavaScript uses one number type for:

  • Integers
  • Decimals

Boolean

Booleans represent:

True

False

Example:

let isLoggedIn =
true;

Example:

let isAdmin =
false;

Booleans are heavily used in conditions.


Real World Example

Food Delivery App:

let orderDelivered =
true;

If delivered:

Show Success Message

If false:

Show Tracking Screen

Undefined

When a variable exists but has no value.

Example:

let phone;

Output:

console.log(phone);

Result:

undefined

JavaScript is saying:

The variable exists, but no value has been assigned.


Null

Null means:

Intentional Empty Value

Example:

let profilePhoto =
null;

Meaning:

No photo currently exists

This is different from undefined.


BigInt

Used for very large numbers.

Example:

let amount =
12345678901234567890n;

Mostly used in specialized applications.


Symbol

Creates unique identifiers.

Example:

const id =
Symbol();

Used in advanced JavaScript development.


Non-Primitive Data Types

These are more complex.

The two most important are:

Objects

Arrays

Objects

Objects store related information.

Example:

const user = {

name: "Aqad",

age: 25,

city: "Bangalore"

};

Output:

User Information

stored together.


Arrays

Arrays store multiple values.

Example:

const technologies = [

"JavaScript",

"Node.js",

"MySQL"

];

Output:

[
JavaScript
Node.js
MySQL
]

Arrays become extremely important in modern applications.


How JavaScript Knows Data Types

JavaScript is:

Dynamically Typed

Meaning:

let value = "Aqad";

Later:

value = 100;

Still valid.

JavaScript automatically adjusts.


Checking Data Types

Use:

typeof

Example:

typeof "Hello";

Output:

string

Example:

typeof 100;

Output:

number

Example:

typeof true;

Output:

boolean

Real Project Example

Imagine AQAD Marketplace.

Product:

const product = {

title:
"Apple Juice",

price:
10,

inStock:
true

};

Here:

title → string

price → number

inStock → boolean

Multiple data types work together.


Common Beginner Mistakes

Forgetting Quotes

Wrong:

let name = Aqad;

Correct:

let name = "Aqad";

Using const Everywhere

Wrong:

const count = 0;

count = 1;

Produces error.

Use:

let count = 0;

Confusing null and undefined

Undefined:

No value assigned

Null:

Empty value intentionally assigned

Memory Visualization

Example:

const userName =
"Aqad";

let age =
25;

Memory:

userName

"Aqad"

age

25

JavaScript continuously stores and retrieves values from memory.

Understanding this concept will become very important when we learn:

  • Execution Context
  • Call Stack
  • Closures
  • Memory Management

Key Takeaways

Remember:

✅ Variables store information

✅ let allows reassignment

✅ const prevents reassignment

✅ Avoid var in modern projects

✅ Strings store text

✅ Numbers store numeric values

✅ Booleans store true or false

✅ Objects store related data

✅ Arrays store collections of data


Conclusion

Variables are the foundation of every JavaScript application. They allow developers to store, retrieve, and manipulate information efficiently. Understanding variables and data types is like learning how a warehouse stores products—once you understand the storage system, everything else becomes easier.

In the next chapter, we'll learn Operators and Type Conversion, where JavaScript begins performing calculations, comparisons, and decision-making. This is where your programs start becoming intelligent instead of simply storing data.

Post a Comment

0 Comments