First Thought: An array is a numbered, ordered list of items.
Think of it like a list of high scores, a grocery list, or a train with numbered cars. Each item has a specific position.
A. Creating an Array
The simplest and most common way is using square brackets [].
// An empty array (an empty list)
let emptyList = [];
// An array of numbers
let scores = [98, 85, 100, 92];
// An array of strings
let names = ["Alice", "Bob", "Charlie"];
// An array can hold different data types
let mixedData = [10, "hello", true, null];
B. The .length Property
Every array has a .length property that tells you how many items are in it.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits.length); // 3
This property is automatically updated when you add or remove items.
C. Accessing and Changing Elements (Zero-Based Indexing) To read or change an item, you use its position number, called an index.
CRITICAL RULE: Array indexing in JavaScript starts at 0.
0.1.let fruits = ["Apple", "Banana", "Cherry"];
// Index: 0 1 2
// Accessing (reading) an element
console.log(fruits[0]); // "Apple"
// Changing (writing) an element
fruits[1] = "Blueberry";
console.log(fruits); // ["Apple", "Blueberry", "Cherry"]
// Getting the last element
console.log(fruits[fruits.length - 1]); // "Cherry"
If you try to access an index that doesn't exist, you get undefined.
These methods change the original array.