Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.1 — Creating and Reading Arrays
JavaScript · Example 3 of 3

length can be written, and arrays are objects

Setting length smaller deletes items for good. Writing past the end leaves holes. const only protects the variable, not the array's contents.

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.

01

Create an array of five weekdays. Print the first day, the last day using at, and the number of days.

02

Use Array.from to build an array of the squares of 1 to 5: [ 1, 4, 9, 16, 25 ].

03

Given const word = "level";, make an array of its letters and print the middle letter using length.

Explain it without notes

01

Why do arrays start at index 0, and how do you safely read the last element?

02

What is the difference between Array(3), Array.of(3) and [3]?

03

What does Array.from accept, and why is it useful with querySelectorAll?

04

How do you check whether a value is an array, and why doesn't typeof work?

length can be written, and arrays are objects

Console

(nothing was printed)

Expected console output

[ 1, 2 ]
[ 1, 2, <empty>, <empty>, 9 ] 5
object true
[ 'allowed' ]