Create, read and count
at() is ES2022; everything else here has worked since the first versions of JavaScript. The page uses an <ol>, which numbers from 1, while the indexes start at 0.
An array is an ordered list of values; you create one with square brackets, read items by their position number (the index, starting at 0), get the count from length, and build arrays from other things with Array.from and Array.of.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Create an array of five weekdays. Print the first day, the last day using at, and the number of days.
Use Array.from to build an array of the squares of 1 to 5: [ 1, 4, 9, 16, 25 ].
Given const word = "level";, make an array of its letters and print the middle letter using length.
Explain it without notes
Why do arrays start at index 0, and how do you safely read the last element?
What is the difference between Array(3), Array.of(3) and [3]?
What does Array.from accept, and why is it useful with querySelectorAll?
How do you check whether a value is an array, and why doesn't typeof work?
Preview
Console
Nothing logged yet.Expected console output
[ 'apple', 'banana', 'cherry' ]
3
apple cherry
undefined
cherry
cherry
undefined