Introduction to Arrays
An array is an ordered list of values stored under a single variable name. Instead of creating student1, student2 and student3, you keep all the names in one array and reach each one by its position. Arrays are the standard way to hold lists in JavaScript, whether that list is shopping-cart items, API results or the cells of a table.
Creating an array and reading items
const fruits = ['apple', 'banana', 'mango'];
console.log(fruits[0]); // 'apple' first item, index 0
console.log(fruits[2]); // 'mango'
console.log(fruits.length); // 3
console.log(fruits[fruits.length - 1]); // last item: 'mango'
console.log(fruits.at(-1)); // also the last item
console.log(fruits[5]); // undefined (no error)
fruits[1] = 'guava'; // replace an item
fruits.push('papaya'); // add to the end
fruits.unshift('kiwi'); // add to the start
const last = fruits.pop(); // remove from the end
console.log(fruits); // ['kiwi', 'apple', 'guava', 'mango']
const mixed = [42, 'text', true, null, { id: 1 }, [1, 2]];
console.log(Array.isArray(mixed)); // true
console.log(typeof mixed); // 'object'
How indexes work
Positions start at 0, not 1. In an array of three items, valid indexes are 0, 1 and 2, and length is 3. That is why the last item is always at length - 1. Reading an index that does not exist returns undefined instead of throwing, so typos in an index fail quietly. The at() method accepts negative numbers to count from the end.
What makes JavaScript arrays flexible
- Any type, mixed freely. One array can hold numbers, strings, objects and even other arrays. In practice, keep each array to one kind of thing so code that loops over it stays simple.
- Dynamic size. Arrays grow and shrink as you add and remove items. There is no fixed length to declare.
- They are objects.
typeofreports"object", so useArray.isArray()when you need to check for an array.
Looping through an array
You will often need to do something with every item. Three common ways:
| Approach | Best for |
|---|---|
for (const fruit of fruits) { ... } | Simple, readable loops over values. |
fruits.forEach((fruit, i) => { ... }) | Running code for each item with its index. |
for (let i = 0; i < fruits.length; i++) | When you need full control, such as skipping or stepping backwards. |
Later lessons cover methods like map, filter and reduce, which build new arrays or values from existing ones.
Surprises with const and copying
Many beginners expect const fruits = [...] to make the list unchangeable. It does not. const stops the variable from pointing to a different array, but the array's contents can still change with push, pop or index assignment.
The second surprise is copying. const basket = fruits; does not create a new array. Both names point to the same list, so basket.push('lime') changes fruits too. To make an independent copy, use the spread syntax, const basket = [...fruits];. That copy is shallow: objects inside the array are still shared.
A third, rarer trap: assigning to a far index, like fruits[10] = 'fig', creates empty slots and sets length to 11. Use push to add items so the array stays packed.
Things to remember
Arrays hold ordered values starting at index 0, with the last at length - 1. Add and remove with push, pop, unshift and shift, loop with for...of or forEach, check type with Array.isArray, and copy with spread instead of plain assignment.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.