01 / 21
A B C D 0 1 2 3

Array Creation
& Access

Learn to create arrays, access elements by index, and update values in JavaScript: the foundation of working with collections of data.

JavaScript

What Is an Array?

An array is an ordered list of values stored in a single variable. Instead of creating ten separate variables for ten scores, you store them all in one place: each value sits in a numbered slot called an index.

92 87 74 95 ... index 0 index 1 index 2 index 3 scores

Why Arrays Matter

Organization
Group related data under one name instead of dozens of separate variables.
Loops
Process every element automatically with a loop, no matter the array's size.
Built-in Methods
JavaScript provides powerful tools like .push(), .pop(), .map(), and more.

Anatomy of an Array Literal

let colors = [ "red" , "green" , "blue" ] ; keyword name open [ elements (comma-separated) close ]

Creating Arrays

JS
// Array of numbers
let scores = [92, 87, 74, 95];

// Array of strings
let fruits = ["apple", "banana", "cherry"];

// Mixed types (valid, but less common)
let mix = [42, "hello", true];

// Empty array
let empty = [];

Zero-Based Indexing

JavaScript arrays are zero-indexed: the first element is at index 0, the second at index 1, and so on. The last element is at index \(length - 1\).

"apple" "banana" "cherry" "date" [0] [1] [2] [3] .length = 4 (last index = 3)

Accessing Elements

JS
let fruits = ["apple", "banana", "cherry"];

fruits[0];   // "apple"   (first element)
fruits[1];   // "banana"  (second element)
fruits[2];   // "cherry"  (last element)
fruits[3];   // undefined (out of bounds)

Updating Elements

JS
let colors = ["red", "green", "blue"];

// Change the second element
colors[1] = "yellow";

console.log(colors);
// ["red", "yellow", "blue"]

Assign a new value using the same bracket notation on the left side of =. This overwrites the old value: there is no undo.

Arrays vs. Objects

Array Object
Access by Numeric index Named key (string)
Syntax arr[0] obj.name or obj["name"]
Order Guaranteed insertion order Not guaranteed (mostly)
Has .length Yes No (use Object.keys)
Best for Lists of similar items Entities with named properties

The .length Property

JS
let pets = ["cat", "dog", "fish"];

pets.length;              // 3

// Access the last element dynamically
pets[pets.length - 1];     // "fish"

// Loop through every element
for (let i = 0; i < pets.length; i++) {
  console.log(pets[i]);
}

How to Apply It

1
Declare a variable with let or const.
2
Assign an array literal using [ ] with comma-separated values.
3
Access any element with arrayName[index]. Remember: first index is 0.
4
Update an element by assigning to its index: arrayName[index] = newValue.
5
Use .length to find the size and to loop through every element safely.

Example 1: Building a Grocery List

JS
let groceries = ["milk", "eggs", "bread"];

// Read the first item
console.log(groceries[0]); // "milk"

// Change "eggs" to "butter"
groceries[1] = "butter";

// Check the result
console.log(groceries);
// ["milk", "butter", "bread"]

Example 2: Dynamic Index Access

JS
let temps = [68, 72, 75, 70, 69];

// Use a variable as the index
let day = 3;
console.log(temps[day]);      // 70

// Get the last temperature
let last = temps[temps.length - 1];
console.log(last);            // 69

// Sum all temperatures
let sum = 0;
for (let i = 0; i < temps.length; i++) {
  sum += temps[i];
}
console.log(sum);  // 354

Example 3: Nested Arrays

JS
let grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

grid[0];        // [1, 2, 3]    (first row)
grid[1][2];     // 6            (row 1, col 2)
grid[2][0];     // 7            (row 2, col 0)

// Update the center cell
grid[1][1] = 99;
console.log(grid[1]); // [4, 99, 6]

Common Mistakes

✕ Off-by-One Error
Using arr[arr.length] instead of arr[arr.length - 1] to get the last element. The first returns undefined.
✕ Starting at Index 1
Forgetting that arrays start at index 0. Writing arr[1] gives the second element, not the first.
✕ Using Parentheses
Writing arr(0) instead of arr[0]. Parentheses call a function; brackets access an index.

Quick Reference Cheat Sheet

Operation Syntax Example
Create let a = [...] let a = [1, 2, 3]
Read a[index] a[0] → 1
Update a[index] = val a[1] = 99
Length a.length a.length → 3
Last item a[a.length - 1] a[2] → 3
Nested a[r][c] grid[1][2]

Question 1 of 5

Given let arr = ["x", "y", "z"];, what does arr[1] return?

Question 2 of 5

What is the correct way to change the third element of an array called items to "new"?

Question 3 of 5

Given let nums = [10, 20, 30, 40];, what does nums[nums.length] return?

Question 4 of 5

What does the following code log?

const data = [5, 10, 15];
data[0] = 99;
console.log(data);

Question 5 of 5

Given:

let matrix = [[1,2],[3,4],[5,6]];

What does matrix[2][0] return?