JavaScript Roadmap — Day 31: Introduction to Arrays — Storing Multiple Values
Introduction to Arrays — Storing Multiple Values
So far, we've stored single values in variables. But what if you need to store a list of items? Arrays are the answer — they let you store multiple values in one variable, making data management powerful and efficient.
Imagine you need to store a list of 10 student names. Without arrays, you'd need 10 separate variables. With arrays, you use just one:
Arrays are one of the most important data structures in JavaScript. They allow you to store, organize, and manipulate collections of data efficiently. Today, we'll start with the basics — creating arrays, accessing elements, and understanding array properties. This is the foundation for all advanced array methods you'll learn in the coming days.
1. The Problem — Managing Multiple Values
Imagine you're building a shopping cart. A customer can add multiple items. Without arrays, you'd need a separate variable for each item — which is impossible because you don't know how many items they'll add. Arrays solve this by letting you store any number of items in a single, ordered list.
Think of an array like a train with multiple compartments. The train is your array variable, and each compartment holds a value. You can add more compartments, remove them, or rearrange them — all while keeping the train (array) intact.
💡 The insight: Arrays are ordered collections. Each value has a position (index) starting from 0. This order matters — you can access items by their position, loop through them in order, and sort them.
2. What Are Arrays? — Ordered Lists of Values
An array is a special type of object used to store ordered collections. You can put any type of data in an array — numbers, strings, booleans, objects, even other arrays! Arrays are zero-indexed, meaning the first element is at position 0, the second at 1, and so on.
Question 1: What index position does the first element in an array have?
3. Creating Arrays — Two Ways
There are two main ways to create arrays in JavaScript: array literals (preferred) using square brackets [], and the Array constructor using new Array(). The array literal is shorter, cleaner, and almost always the better choice.
4. Accessing and Modifying Array Elements
Use bracket notation array[index] to access or modify array elements. Remember that indexes start at 0. You can also change existing elements by assigning a new value to an index.
Question 2: Given let arr = [10, 20, 30, 40], what is arr[2]?
Question 3: What happens when you access an index that doesn't exist in an array?
5. The length Property — How Many Items?
Every array has a length property that tells you how many elements are in the array. This is incredibly useful for looping through arrays (which we'll cover soon). The length property updates automatically when you add or remove elements.
Question 4: If an array has 7 elements, what is the index of the last element?
6. Common Mistakes to Avoid
7. Try It Yourself — Array Playground
Experiment with arrays below. Add, modify, and view array elements in real time.
8. Practice Tasks
Task 1 — Easy: Create Your First Array
Create an array called favoriteFoods with 5 of your favorite foods. Then log the first and last items using bracket notation.
Task 2 — Medium: Modify the Array
Start with let numbers = [10, 20, 30, 40, 50]. Change the third element to 99. Then add a new element 60 at the end. Finally, log the array and its length.
Task 3 — Hard: Array Analyzer
Write a function analyzeArray(arr) that logs: the length of the array, the first element, the last element, and whether the array contains the value "JavaScript". Test with different arrays.
Create an array called myArray with any 5 items. Then write code to swap the first and last items. Log the array before and after.
🔍 View Solution
let myArray = [1, 2, 3, 4, 5];
console.log("Before:", myArray);
let temp = myArray[0];
myArray[0] = myArray[myArray.length - 1];
myArray[myArray.length - 1] = temp;
console.log("After:", myArray);
- Arrays store multiple values in a single variable
- Zero-indexed — first element is at index
0 - Use bracket notation
array[index]to access/modify elements - The length property tells you how many elements are in the array
- Always prefer array literals
[]overnew Array() - Accessing an index that doesn't exist returns undefined (not an error)
❓ Q: How do you create an array in JavaScript?
💡 A: Using array literals: let arr = [1, 2, 3];
❓ Q: What is the index of the first element in an array?
💡 A: 0 (arrays are zero-indexed).
❓ Q: How do you get the number of elements in an array?
💡 A: Using the length property: arr.length.