Ch 9 · Arrays — Lists of Data · JavaScript
Topic 9 of 13 in JavaScript — Complete Tutorial (Beginner) — 3 lessons.
What is an Array?
An array is an ordered list of values stored together under a single variable name. Before arrays, if you wanted to store five students' marks, you would need five separate variables: mark1, mark2, mark3, mark4, mark5. If you had fifty students, you would need fifty variables — and every loop and calculation would have to reference each one individually. An array solves this completely: you store all fifty marks together under one name like marks, and you can process all of them with a single loop.
Common Array Methods
Arrays come with a powerful set of built-in methods. An important distinction to understand is that some methods modify the original array (they are called "mutating" methods), while others return a new array or value and leave the original completely untouched. For example, push adds to the array and changes it in place, while slice extracts a portion and returns a fresh copy — the original is unchanged. Always check which type a method is before using it, especially when you need to preserve the original data.
Looping Through Arrays
The most common thing you will ever do with an array is process every element — print each item, add up values, check each entry for a condition. You do this by combining a loop with an array. The for-of loop is the cleanest and most readable way to do this in JavaScript: you write for (let item of array) and JavaScript automatically gives you each element in turn, from index 0 to the last, without you needing to manage index numbers at all. When you need both the element and its index at the same time, use a regular for loop with i going from 0 to array.length - 1.
All topics in JavaScript Beginner
- Introduction to Programming & JavaScript
- Your First JavaScript Program
- Variables — Your Data Containers
- Data Types — Kinds of Values
- Operators & Expressions
- Conditional Statements
- Loops & Iterations
- Functions
- Arrays — Lists of Data
- Objects & Classes
- Strings — Working with Text
- Error Handling
- Mini-Projects & Practice