Challenge 1 +15 XP
Complete the function to return the last element of the given array, no matter how long it is.
- Test 1 — expects "3\n"
Show solution (0 XP)
const arr = [1, 2, 3];
console.log(arr[arr.length - 1]); In programming, we often need to store a list of related items—like a list of user names, a sequence of high scores, or the colors in a palette. An array is a data structure that lets you hold multiple values in a single variable.
You create an array using an array literal, which is simply a pair of square brackets [] enclosing a comma-separated list of values. Arrays can hold any type of data, and they can even hold mixed data types!
const colors = ["red", "green", "blue"];
console.log(colors[0]);red
To get a value out of an array, you use its index. JavaScript arrays are zero-indexed. This means the counting starts at 0, not 1! The very first element of an array is at index 0, the second is at index 1, and so on.
const letters = ["a", "b", "c"];
console.log(letters[2]);c
Here is a diagram showing how indices map to elements in an array:
Every array has a length property that tells you exactly how many items are inside it. Since indices start at 0, the highest index in an array is always length - 1.
const nums = [10, 20, 30];
console.log(nums.length);3
What happens if you try to access an index that is completely empty? In some languages, this will immediately crash your program. Let’s see what JavaScript does.
Read the code. What exactly will it print? Commit to an answer before you look.
const arr = [1, 2];
console.log(arr[5]);undefined
JavaScript simply returns undefined. It does not throw a runtime error. This means you must be very careful when writing loops, because reading outside the array won’t crash—it will just silently give you undefined, which might cause subtle bugs later on!
Arrays in JavaScript are dynamic; they can grow and shrink.
The push() method adds one or more elements to the end of the array. It also returns the new length of the array.
The pop() method removes the last element from the array and returns that removed element.
const stack = ["x"];
stack.push("y");
console.log(stack[1]);y
const stack2 = ["x", "y"];
const removed = stack2.pop();
console.log(removed);y
Let’s look at exactly how push and pop mutate the array over time. Run this step-through visualizer to watch the array change.
const fruits = [];
fruits.push("apple");
fruits.push("banana");
fruits.pop();pop() on an array that is already empty, it doesn’t throw an error. It simply returns undefined and leaves the array’s length at 0.mixed[2][0].What happens if you try to access an array index that doesn't exist?
It returns undefined — In JavaScript, accessing an out-of-bounds index returns undefined rather than throwing an error.
What is the highest valid index in an array?
The array's length minus 1 — Because JavaScript arrays are zero-indexed, the first item is at index 0 and the last item is at length - 1.
Can you add elements to an array declared with 'const'?
Yes, const prevents reassignment, but the array itself can still be mutated — const only prevents reassigning the variable to a entirely new value. The array itself can still be changed using methods like push().
What does the push() method return?
The new length of the array — The push() method adds elements to the array and returns the array's new length.
Complete the function to return the last element of the given array, no matter how long it is.
const arr = [1, 2, 3];
console.log(arr[arr.length - 1]); Bug Hunt: This code is supposed to add "apple" to the end of the array and log the array. Why does it log a number instead?
This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.
const arr = ["banana"];
arr.push("apple");
console.log(arr[0]);
console.log(arr[1]);