What is a data structure? Arrays

What a data structure and an array are; index, length.

1. What is this?

A data structure organises values so you can use them efficiently. An array is the simplest: an ordered list where each slot has an index (position) starting at 0. let scores = [8, 5, 9] holds three numbers; scores[0] is 8; scores.length is 3. A loop visits each slot in turn.

2. Why do we use it?

Scores, todos, messages — ordered collections are everywhere. Arrays plus loops process them.

3. Syntax breakdown

  • let scores = [8, 5, 9];

    Creates an array of three numbers.

  • scores[0]

    First item (index 0). scores[2] is the last here.

  • scores.length

    How many items: 3.

5. Common mistakes

  • Starting at index 1 — arrays start at 0, so the last index is length - 1.
  • Reading scores[3] in a 3-item array — undefined, a classic off-by-one.
  • Forgetting that length is a property (no parentheses).

Practice task

Given [4, 7, 2], print the average (total / length).

Hint

Sum with a loop, then console.log(total / scores.length).

Try it below — tests run automatically when you press Run.

dsa

Output

Press Run to see output.

Mini quiz

3 questions. Answer, then check.

  1. 1. What index holds the first array item?

  2. 2. What is scores.length?

  3. 3. What is a data structure?

4. Example — edit and run

dsa

Output

Press Run to see output.

Expected output: First: 8