JavaScript Arrays 101
Imagine storing a shopping list in JavaScript like this:
const fruit1 = "Apple";
const fruit2 = "Banana";
const fruit3 = "Mango";
const fruit4 = "Orange";
This works for a few values.
But what if you need to store:
100 student marks
500 product names
Thousands of messages
Managing separate variables becomes difficult.
That’s where:
Arrays
become useful.
Arrays help us store multiple values together in an organized way.
In this beginner-friendly guide, you’ll learn:
What arrays are
Why arrays are needed
How to create arrays
Accessing elements using indexes
Updating array values
Array length property
Looping through arrays
By the end, you’ll understand one of the most important data structures in JavaScript.
What Are Arrays?
An array is:
A collection of multiple values stored together in order.
Real-World Example
Imagine a fruit basket containing:
Apple
Banana
Mango
Orange
Instead of creating separate variables for each fruit, we can store them inside one array.
Array Example
const fruits = [
"Apple",
"Banana",
"Mango",
"Orange"
];
Now all values are stored together.
Why Arrays Are Needed
Without arrays:
const mark1 = 90;
const mark2 = 85;
const mark3 = 95;
This becomes difficult to manage for large data.
Arrays solve this problem by grouping related values together.
Arrays as Ordered Collections
Arrays store values:
In order
Each value has a position called an:
Index
Visual Representation of an Array
Index: 0 1 2 3
┌──────┬────────┬───────┬────────┐
Value: │Apple │Banana │ Mango │ Orange │
└──────┴────────┴───────┴────────┘
Important Rule: Index Starts From 0
In JavaScript:
First element → index
0Second element → index
1
This is very important.
Why Index Starts from 0
Most programming languages use zero-based indexing because it matches memory offset calculations internally.
For beginners, simply remember:
First element = index 0
Creating Arrays in JavaScript
Arrays are created using square brackets:
[]
Basic Syntax
const arrayName = [
value1,
value2,
value3
];
Example
const colors = [
"Red",
"Blue",
"Green"
];
Arrays Can Store Different Types
JavaScript arrays can store:
Strings
Numbers
Booleans
Example:
const mixed = [
"Hitesh",
22,
true
];
Accessing Array Elements
Elements are accessed using indexes.
Syntax
arrayName[index]
Example
const fruits = [
"Apple",
"Banana",
"Mango"
];
console.log(fruits[0]);
Output:
Apple
Accessing More Elements
console.log(fruits[1]);
console.log(fruits[2]);
Output:
Banana
Mango
What Happens If Index Does Not Exist?
console.log(fruits[10]);
Output:
undefined
because index 10 does not exist.
Updating Array Elements
Arrays are mutable, meaning values can be changed.
Example
const fruits = [
"Apple",
"Banana",
"Mango"
];
fruits[1] = "Orange";
console.log(fruits);
Output:
[ 'Apple', 'Orange', 'Mango' ]
Understanding the Change
Original:
Banana
Updated to:
Orange
using index 1.
Array Length Property
Arrays provide a built-in property called:
length
which tells how many elements exist.
Example
const fruits = [
"Apple",
"Banana",
"Mango"
];
console.log(fruits.length);
Output:
3
Why length is Useful
The length property helps with:
Loops
Counting elements
Dynamic operations
Memory-Style Block Diagram
┌───────┬────────┐
│ Index │ Value │
├───────┼────────┤
│ 0 │ Apple │
│ 1 │ Banana │
│ 2 │ Mango │
└───────┴────────┘
Looping Through Arrays
Arrays often contain many elements.
Loops help process them efficiently.
Using for Loop
const fruits = [
"Apple",
"Banana",
"Mango"
];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Output
Apple
Banana
Mango
Understanding the Loop
The loop:
Starts at index
0Accesses each element
Continues until array ends
Loop Visualization
i = 0 → Apple
i = 1 → Banana
i = 2 → Mango
Real-World Examples of Arrays
Arrays are used everywhere in applications.
Examples:
Shopping carts
Product lists
Messages
Notifications
User comments
Example: Task List
const tasks = [
"Study JavaScript",
"Exercise",
"Read book"
];
Example: Student Marks
const marks = [
85,
90,
78,
92
];
Common Beginner Mistakes
1. Forgetting Index Starts at 0
Wrong expectation:
fruits[1] → first element
Correct:
fruits[0] → first element
2. Using Parentheses Instead of Brackets
Wrong:
fruits(0)
Correct:
fruits[0]
3. Accessing Invalid Index
fruits[100]
returns:
undefined
Arrays vs Individual Variables
| Individual Variables | Arrays |
|---|---|
| Hard to manage | Organized |
| Repetitive code | Cleaner code |
| Difficult looping | Easy looping |
| Poor scalability | Better scalability |
Assignment Practice
Try this yourself.
Task
Create an array of 5 favorite movies
Print first element
Print last element
Change one value
Loop through all elements
Example Solution
const movies = [
"Inception",
"Interstellar",
"Avatar",
"Joker",
"Titanic"
];
console.log(movies[0]);
console.log(
movies[movies.length - 1]
);
movies[2] = "Batman";
console.log(movies);
for (let i = 0; i < movies.length; i++) {
console.log(movies[i]);
}
Why Arrays Matter
Arrays are one of the most important data structures in programming.
They are heavily used in:
Frontend development
Backend systems
Databases
APIs
Algorithms
Libraries and frameworks like React and Vue.js frequently work with arrays internally.
Final Thoughts
Arrays help developers store and manage collections of data efficiently.
You learned:
✅ What arrays are ✅ Why arrays are needed ✅ Creating arrays ✅ Accessing elements using indexes ✅ Updating values ✅ Array length property ✅ Looping through arrays
The most important idea to remember is:
Arrays store multiple values in order, and each value has an index starting from 0.
Once you become comfortable with arrays, many advanced JavaScript concepts become much easier to understand.
