π JavaScript Arrays 101

Like to study,fitness freak,my looks is my first priority, hardworking person, like discipline and love to learn new thing
Imagine you want to store:
5 favorite movies
10 student marks
A list of daily tasks
You could create separate variables like this:
let movie1 = "Inception";
let movie2 = "Interstellar";
let movie3 = "Avatar";
But what if you have 100 values?
Thatβs messy.
This is where arrays help.
π What Are Arrays?
An array is a collection of multiple values stored in a single variable.
Think of it like a row of boxes:
[ value, value, value, value ]
Arrays store values in order and each value has a position called an index.
ποΈ How to Create an Array
We use square brackets [] to create an array.
Example: List of Fruits
let fruits = ["Apple", "Banana", "Mango", "Orange"];
Now all fruits are stored inside one variable.
Much cleaner than creating 4 separate variables.
π Visual Representation of an Array
Index: 0 1 2 3
--------------------------------
Value: Apple Banana Mango Orange
β Important: Indexing starts from 0, not 1.
Thatβs very important in JavaScript.
π Accessing Elements Using Index
To access a value, use its index number.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana
Structure:
fruits[index]
If you try to access a non-existing index:
console.log(fruits[10]);
It will return:
undefined
π Updating Array Elements
You can change values using index.
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Grapes";
console.log(fruits);
Before:
["Apple", "Banana", "Mango"]
After:
["Apple", "Grapes", "Mango"]
Arrays are mutable β meaning values can be changed.
π The length Property
To know how many elements are in an array, use .length.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length);
Output:
3
Useful when looping through arrays.
π Looping Over Arrays
To print all elements, we can use a simple for loop.
let fruits = ["Apple", "Banana", "Mango"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
How it works:
Start from index 0
Continue until
i < fruits.lengthPrint each element
π§ Memory-Style Block Diagram
Think of array storage like this:
fruits
β
-------------------------
| Apple | Banana | Mango |
-------------------------
0 1 2
Each value occupies a position in memory.
π Individual Variables vs Array
β Without Array
let mark1 = 85;
let mark2 = 90;
let mark3 = 78;
Hard to manage.
β With Array
let marks = [85, 90, 78];
Cleaner. Scalable. Easier to loop.
π‘ Final Thoughts
Arrays are one of the most important data structures in JavaScript.
They are used in:
Storing API data
Managing lists
Working with loops
Building real-world applications
If you understand:
Indexing
Updating
Length
Looping
Youβre building strong fundamentals.
Advanced array methods (map, filter, reduce) come next β but mastering the basics first is very important.
