JavaScript Arrays 101

When beginners start learning JavaScript, one common challenge is managing multiple values in a program.
Imagine a simple situation.
You want to store the names of your favorite fruits in a program.
Without arrays, you might write something like this:
let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";
let fruit4 = "Orange";
let fruit5 = "Grapes";
This works, but it creates several problems:
Too many variables
Hard to manage data
Difficult to perform operations on all values
Code becomes messy as data grows
Now imagine if we had 100 fruits instead of 5.
Creating 100 variables would be impractical.
This is exactly where arrays become extremely useful.
What is an Array in JavaScript?
An array is a special data structure in JavaScript that allows us to store multiple values in a single variable.
Think of an array as a container or list that holds multiple items.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];
Here:
fruits→ variable nameThe square brackets
[]indicate an arrayThe values inside are called elements
So instead of using 5 variables, we used one array variable.
Why Do We Need Arrays?
Arrays are important because they help us manage collections of related data.
Common real-world examples include:
List of student marks
List of items in a shopping cart
List of tasks in a to-do list
List of songs in a playlist
List of products in an e-commerce website
Example: storing marks of a student
Without array:
let mark1 = 85;
let mark2 = 90;
let mark3 = 78;
let mark4 = 88;
let mark5 = 92;
With array:
let marks = [85, 90, 78, 88, 92];
Clearly, arrays make the code:
Cleaner
Shorter
Easier to manage
How Arrays Work Internally
You can imagine an array as boxes placed next to each other in memory.
Each box stores a value and has a number called an index.
Example array:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
Visual representation:
Index: 0 1 2 3
----- ----- ----- -----
Apple Banana Mango Orange
----- ----- ----- -----
Important rule:
Array indexing always starts from 0.
So:
Apple → index 0
Banana → index 1
Mango → index 2
Orange → index 3
This rule is very important in programming.
How to Create an Array
In JavaScript, arrays are created using square brackets [].
Example 1: Fruits Array
let fruits = ["Apple", "Banana", "Mango"];
Example 2: Numbers Array
let numbers = [10, 20, 30, 40];
Example 3: Names Array
let students = ["Rahul", "Amit", "Sneha", "Priya"];
What Type of Data Can Arrays Store?
JavaScript arrays are very flexible. They can store different types of values.
1. Strings
let colors = ["Red", "Blue", "Green"];
2. Numbers
let marks = [80, 90, 75, 88];
3. Booleans
let answers = [true, false, true];
4. Mixed Data Types
JavaScript even allows mixing types.
let data = ["Virat", 32, true];
Although possible, beginners should usually keep similar data types in one array.
Accessing Elements Using Index
To access elements from an array, we use index numbers inside square brackets.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
Access elements:
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
Output:
Apple
Banana
Mango
Explanation:
fruits[0]→ first elementfruits[1]→ second elementfruits[2]→ third element
Accessing the Last Element
Sometimes we want the last element of an array.
We can do this using the length property.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits[fruits.length - 1]);
Output:
Orange
Explanation:
fruits.length→ total number of elementslength - 1→ last index
Updating Elements in an Array
Arrays are mutable, meaning their values can be changed.
Example:
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Grapes";
console.log(fruits);
Output:
["Apple", "Grapes", "Mango"]
Here we changed:
Banana → Grapes
Array Length Property
The length property tells us how many elements are inside an array.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.length);
Output:
4
The length property is very useful when:
Iterating through arrays
Finding the last element
Checking array size
Looping Through an Array
Often we want to process every element in the array.
The best way is to use a loop.
Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Output:
Apple
Banana
Mango
Orange
Explanation of the loop:
i = 0→ start from index 0i < fruits.length→ continue until the last elementi++→ move to the next index
Practical Assignment Example
Now let's implement the assignment step by step.
Step 1: Create an Array of 5 Favorite Movies
let movies = [
"Inception",
"Interstellar",
"The Dark Knight",
"Avengers",
"Titanic"
];
Step 2: Print the First and Last Movie
console.log("First Movie:", movies[0]);
console.log("Last Movie:", movies[movies.length - 1]);
Output:
First Movie: Inception
Last Movie: Titanic
Step 3: Change One Value
movies[2] = "Spider-Man";
console.log(movies);
Updated array:
["Inception", "Interstellar", "Spider-Man", "Avengers", "Titanic"]
Step 4: Loop Through the Array
for (let i = 0; i < movies.length; i++) {
console.log(movies[i]);
}
Output:
Inception
Interstellar
Spider-Man
Avengers
Titanic
Visual Diagram of an Array
Movies Array
Index: 0 1 2 3 4
------- ------- ------- ------- -------
Inception Interstellar Spider-Man Avengers Titanic
------- ------- ------- ------- -------
Each element has a position number (index) used to access it.
Common Beginner Mistakes
1. Forgetting that index starts from 0
Wrong:
movies[1] → first element ❌
Correct:
movies[0] → first element ✅
2. Accessing an index that doesn't exist
Example:
console.log(movies[10]);
Output:
undefined
This happens because the index does not exist.
Conclusion
Arrays are one of the most fundamental and powerful concepts in JavaScript.
In this article we learned:
What arrays are
Why arrays are important
How to create arrays
How indexing works
How to access elements
How to update elements
The
lengthpropertyHow to loop through arrays
Understanding arrays well will make it easier to work with:
Lists
Data collections
Real-world applications
Web development tasks
Mastering arrays is an essential step toward becoming a skilled JavaScript developer.
Happy coding 🚀