Hello everyone! If you are learning JavaScript, today we are going to talk about a topic you will use every single day—Arrays.
Imagine you need to bring groceries from the market. Would you carry a separate bag for every single item? No, right? You would put everything into one big shopping bag. In JavaScript, Arrays work exactly like that big bag. Let's understand this in detail!
1. What Are Arrays and Why Do We Need Them?
An array is a data structure used to store multiple values in a single, ordered list.
Why do we need them? Suppose you want to store the names of 3 friends. Without an array, you would have to create separate variables for each:
JavaScript
let friend1 = "Aman";
let friend2 = "Rahul";
let friend3 = "Pooja";
What if you had 100 friends? The code would get very long and repetitive! This is where arrays help us. We can pack all of them into a single list:
JavaScript
let friends = ["Aman", "Rahul", "Pooja"];
See? Done in just one line!
2. How to Create an Array ️
Creating an array in JavaScript is very easy. We simply use square brackets [] and place a comma , between the items.
Let's create a simple array of fruits:
JavaScript
let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits);
3. Accessing Elements (Zero-Based Indexing)
Now, suppose I only want to pick "Mango" out of this array. How do I do that?
There is a small twist here. We humans start counting from 1, but JavaScript counting starts from 0! This position number is called an Index.
Index
0is: "Apple"Index
1is: "Banana"Index
2is: "Mango"
To print a specific element, we write the array name and its index inside square brackets:
JavaScript
let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits[0]); // Output: Apple
console.log(fruits[2]); // Output: Mango
4. Updating Elements ️
Arrays are very flexible. If you want to change a value, you can update it directly using its index.
Suppose I want to replace "Banana" with "Grapes":
JavaScript
let fruits = ["Apple", "Banana", "Mango"];
// "Banana" is at index 1, let's update it
fruits[1] = "Grapes";
console.log(fruits);
// Output: ["Apple", "Grapes", "Mango"]
5. Finding Array Length (Length Property)
If you want to know how many total items are inside your array, you can use the length property.
JavaScript
let tasks = ["Read book", "Write code", "Drink water"];
console.log(tasks.length); // Output: 3
Note: The length property always counts the total number of items (starting from 1), whereas indexing always starts from 0.
6. Looping Over Arrays (Basic Looping)
When you have a large list and you want to print all the items one by one, you don't need to write console.log() over and over again. We can use a basic for loop:
JavaScript
let marks = [85, 90, 78, 92];
// The loop will start at 0 and run until the end of the array's length
for (let i = 0; i < marks.length; i++) {
console.log("Marks: " + marks[i]);
}
/* Output:
Marks: 85
Marks: 90
Marks: 78
Marks: 92
*/
Practice Time: Assignment For You!
To make sure your array concepts are crystal clear, try completing this small task today:
Create an array containing the names of your 5 favorite movies.
Print the first and last movie names to the console. (Hint: the last element's index is always
arrayName.length - 1).Update one movie's name to something else and print the updated array.
Use a
forloop to go through the array and print all the movie names one by one.
Open up VS Code and try coding this yourself. Happy Coding!
