JS Arrays
In this page:
const arrayName = [item1, item2, item3];
arrayName[index];
arrayName.length;
Creating Arrays
Arrays are created with square brackets [ ], containing a comma-separated list of values, which can be numbers, strings, objects, or even other arrays, all inside the same array.
Example: Creating Arrays
// Declare the constant `numbers`, set to `[1, 2, 3]`
// Declare the constant `numbers`, set to `[1, 2, 3]`
const numbers = [1, 2, 3];
// Declare the constant `mixed`, set to `["text", 5, true]`
// Declare the constant `mixed`, set to `["text", 5, true]`
const mixed = ["text", 5, true];
// Print `numbers, mixed` to the console
// Print `numbers, mixed` to the console
console.log(numbers, mixed);
Accessing Elements
Array elements are accessed using square brackets with a zero-based index, arrayName[0] retrieves the first element, arrayName[1] the second, and so on. Trying to read an index that doesn't exist returns undefined instead of throwing an error.
Example: Accessing Elements
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]);
console.log(fruits[5]); // undefined, index doesn't exist
Array Length
Every array has a length property that reports how many elements it currently contains, automatically updating whenever elements are added or removed. length is also writable directly, and shrinking it manually will actually delete elements from the end of the array.
Example: Array Length
const items = ["a", "b", "c"];
console.log(items.length);
items.length = 1;
console.log(items); // ["a"], shrinking deletes elements
Changing Elements
An individual element can be changed by assigning a new value directly to its index, arrayName[index] = newValue, which replaces just that one slot without affecting the rest of the array.
Example: Changing Elements
// Declare the constant `colors`, set to `["red", "green", "blue"]`
// Declare the constant `colors`, set to `["red", "green", "blue"]`
const colors = ["red", "green", "blue"];
colors[1] = "yellow";
// Print `colors` to the console
// Print `colors` to the console
console.log(colors);
The Spread Operator With Arrays
The spread operator ... expands an array's elements individually, making it easy to copy an entire array or combine multiple arrays together into one new array.
It creates a new array rather than modifying the original, which is why it's a common tool for avoiding accidental mutation.
Example: The Spread Operator With Arrays
// Declare the constant `original`, set to `[1, 2, 3]`
// Declare the constant `original`, set to `[1, 2, 3]`
const original = [1, 2, 3];
// Declare the constant `copy`, set to `[...original]`
// Declare the constant `copy`, set to `[...original]`
const copy = [...original];
// Declare the constant `combined`, set to `[...original, 4, 5]`
// Declare the constant `combined`, set to `[...original, 4, 5]`
const combined = [...original, 4, 5];
// Print `copy, combined` to the console
// Print `copy, combined` to the console
console.log(copy, combined);
- Forgetting arrays are zero-indexed, the first element is at index 0, not 1.
- Trying to access an index beyond the array's length, which returns undefined rather than throwing an error.
- Confusing array length with the last valid index, the last index is always length minus one.
- Arrays store ordered lists of values, accessed by a zero-based numeric index.
- The length property reports how many elements an array contains, and elements can be changed by assigning to a specific index.
- The spread operator can quickly copy or combine arrays, and dozens of built-in methods manipulate array contents.
Arrays and their core operations are fundamental JavaScript features supported identically in every modern browser.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: