A List of Things
An array is just a list β several values lined up in square brackets [ ] and
separated by commas:
const pets = ["cat", "dog", "fish"];
To pick one item, you use its position number in square brackets. Here's the
surprising part: counting starts at 0, not 1. So pets[0] is "cat" (the
first one), pets[1] is "dog", and so on. It feels odd at first β that's totally
normal!
function secondPet(list) {
return list[1];
}
Your task
Write a function firstItem that takes a list and returns its first item.
So firstItem(["a", "b", "c"]) should give back "a".
β‘ 22 XP
π§± Code blocks
Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.
solution.jsRuns in your browser
Loading editorβ¦
Tests2 to pass
firstItem(["a","b","c"]) gives "a"
firstItem([10, 20, 30]) gives 10
Run your code to check these.