JavaScript Arrays

Understanding Arrays in JavaScript (Beginner Friendly)
When you start learning programming, you often deal with multiple values of the same type. Managing them individually quickly becomes messy. This is where arrays come in.
In this blog, we'll understand:
What arrays are and why we need them
How to create arrays
Accessing elements using index
Updating values
The
lengthpropertyLooping through arrays
Let’s start with a simple example.
Why Do We Need Arrays?
Imagine you want to store a list of fruits.
Without arrays, you might write something like this:
let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";
let fruit4 = "Orange";
This quickly becomes hard to manage if the list grows.
Instead, we can store all values inside one structure called an array.
let fruits = ["Apple", "Banana", "Mango", "Orange"];
Now all fruits are stored together in a single variable.
What is an Array?
An array is a collection of values stored in order.
Example:
let numbers = [10, 20, 30, 40];
Here the array contains four values.
Arrays can store many types of values:
let names = ["Rahul", "Amit", "Priya"];
let marks = [85, 90, 78];
let tasks = ["Study", "Exercise", "Read"];
How to Create an Array
Arrays are created using square brackets [].
Example:
let colors = ["Red", "Green", "Blue"];
Each value inside the array is separated by a comma.
Accessing Elements Using Index
Each element in an array has a position, called an index.
Important rule:
Array indexing starts from 0.
Example:
let fruits = ["Apple", "Banana", "Mango"];
| Index | Value |
|---|---|
| 0 | Apple |
| 1 | Banana |
| 2 | Mango |
Access elements like this:
console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana
console.log(fruits[2]); // Mango
Updating Array Elements
You can change values by using the index.
Example:
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Orange";
console.log(fruits);
Output:
["Apple", "Orange", "Mango"]
The value Banana was replaced with Orange.
The Array Length Property
Arrays have a property called length that tells how many elements are inside the array.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.length);
Output:
4
This means the array contains four elements.
Looping Through Arrays
Often we want to print or process every element in an array.
We can use a for loop.
Example:
let fruits = ["Apple", "Banana", "Mango"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Output:
Apple
Banana
Mango
The loop goes through each index of the array and prints its value.
Summary
Arrays are one of the most important concepts in programming.
Key points:
Arrays store multiple values in one variable
They maintain ordered collections
Index starts from 0
Elements can be accessed and updated
lengthtells the number of elementsLoops help process all elements easily
Once you understand arrays, managing data becomes much simpler and cleaner.
Assignment
Try this small exercise.
Create an array of 5 favorite movies
Print the first and last movie
Change one value in the array
Loop through the array and print all movies

