Array Creation
& Access
Learn to create arrays, access elements by index, and update values in JavaScript: the foundation of working with collections of data.
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.
Why Arrays Matter
.push(), .pop(), .map(), and more.Anatomy of an Array Literal
Creating Arrays
// 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\).
Accessing Elements
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
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
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
let or const.[ ] with comma-separated values.arrayName[index]. Remember: first index is 0.arrayName[index] = newValue..length to find the size and to loop through every element safely.Example 1: Building a Grocery List
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
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
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
arr[arr.length] instead of arr[arr.length - 1] to get the last element. The first returns undefined.arr[1] gives the second element, not the first.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"?
items[2] = "new". Parentheses would try to call items as a function.Question 3 of 5
Given let nums = [10, 20, 30, 40];, what does nums[nums.length] return?
nums.length is 4, but valid indices are 0 through 3. Accessing index 4 is out of bounds and returns undefined. To get the last element, use nums[nums.length - 1].Question 4 of 5
What does the following code log?
const data = [5, 10, 15]; data[0] = 99; console.log(data);
const prevents reassigning the variable, but it does not prevent changing elements inside the array. data[0] = 99 is perfectly valid.Question 5 of 5
Given:
let matrix = [[1,2],[3,4],[5,6]];
What does matrix[2][0] return?
matrix[2] selects the third inner array: [5, 6]. Then [0] selects the first element of that array: 5.