Loading lessons...
Array Basics
Array Basics
An array is an ordered list of values. It is one of the most used data structures in JavaScript.
Creating an array
let fruits = ["apple", "banana", "cherry"];
let numbers = [1, 2, 3, 4, 5];
let mixed = ["hello", 42, true];
Arrays can hold anything
An array can mix strings, numbers, booleans, objects, and even other arrays.
Access items by index
Indexes start at 0:
fruits[0]; // "apple"
fruits[1]; // "banana"
fruits[2]; // "cherry"
Change an item
fruits[1] = "blueberry";
The length property
fruits.length; // 3
The last item
fruits[fruits.length - 1]; // last element
TL;DR
- Arrays are ordered lists in square brackets.
- Indexes start at 0.
- Use [index] to read or write items.
- .length gives the number of items.