Skills practised: Arrays, Working with collections.
04Explanation
An array is an ordered list of values written in square brackets: ["Tea", "Cake", "Juice"]. One variable holds the whole list.
Each value is an element and has a numbered position called an index. Indexes start at 0, so the first element is list[0] and the second is list[1].
list.length tells you how many elements there are. The last element is at list.length - 1, or you can write list.at(-1).
You can change an element by assigning to its index: list[1] = "Scone";. This works even when the array is declared with const, because the variable still points to the same array.
Arrays are objects whose keys are the strings "0", "1", …; length is always one more than the highest index. Assigning arr[10] on a three-element array creates holes (a sparse array), which most methods treat inconsistently.
Reading an index that does not exist returns undefined, not an error. Validate indexes from user input with Number.isInteger(i) && i >= 0 && i < arr.length.
const prevents reassigning the variable, not mutating the array. Use Object.freeze(arr) (shallow) when a list must not change, or prefer non-mutating methods.
Arrays can hold mixed types, but production code keeps one kind of element per array (all numbers, or all objects of the same shape) so that every function can treat elements the same way.
05Definitions
Array
An ordered list of values stored in one variable.
Element
One value stored in an array.
Index
The numbered position of an element, starting at 0.
length
The number of elements in an array.
at()
Reads an element by index; negative numbers count from the end.
A café screen shows today's specials from an array. Staff add and change items, and the screen always shows the first special as the headline and the count underneath.
2. What is the index of the last element in an array of 8 items?
8
7
0
-1
3. What is printed?
const x = [1, 2];
console.log(x[5]);
An error
undefined
null
0
4. Why does this throw TypeError: Assignment to constant variable?
const list = [1, 2];
list = [3, 4];
Arrays cannot change
The whole variable is reassigned
Index 0 does not exist
const arrays are frozen
5. What is printed?
console.log(["a", "b", "c"].at(-2));
3
c
b
undefined
6. Which reads the last element and keeps working when the list grows?
list[2]
list[list.length - 1]
list[list.length]
list[-1]
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the last item: Prints Ship; Not hard-coded
Update the scoreboard: Prints the updated array; scores is updated
Safe element lookup: Prints three results; Rejects 1.5; Reads index 0
ex-026-01: output matches the documented expected output
ex-026-02: output matches the documented expected output
ex-026-03: output matches the documented expected output
ex-026-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-06-arrays-and-collections/lesson-026-creating-and-accessing-arrays/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Arrays hold ordered values in square brackets.
Indexes start at 0; the last is length - 1 or at(-1).
Missing indexes give undefined.
const stops reassignment, not changes to elements.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationarrays
Context: I am learning arrays in JavaScript.
Task: Explain arrays, indexes and length with a playlist example and a drawing of the index positions.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionarrays drill
Context: I have practised arrays.
Task: Ask me eight questions about reading elements by index, including negative at() indexes.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingarrays bug
Context: My program prints undefined when I read the last element of my array.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationarrays practice
Context: I finished the arrays exercises.
Task: Create three exercises that read and change elements of shopping and score arrays.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewarrays review
Context: Here is my code that uses arrays.
Task: Review how I read and update elements and point out any off-by-one risks.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.