Collections and Records: Organize Related Values

Keep each student’s information together

A school stores several students. Each student has a name and marks, and the class has an ordered list of those students. Separate names such as student1Name and student2Name become awkward as the class grows.

A collection groups values, and a record groups related named fields describing one thing. In JavaScript, arrays provide ordered collections and ordinary objects can represent records. An array can contain student records, and a record can contain another collection.

  1. Choose a shape

    Use a record for one student’s named fields and a collection for a class of students.

  2. Read nested data

    Trace an array index and a field name to the exact value they select.

  3. Predict changes

    Explain shared object references, const updates and missing positions without assuming assignment copies a record.

From a class list to one mark

  1. Class collection

    An array stores two student records in order.

    students → [Maya, Ravi]
  2. Select one record

    Index 0 selects the first record, Maya’s.

    students[0] → Maya’s record
  3. Select one field

    The science field in that record contains the mark.

    students[0].science → 35
Positions select items in an ordered collection; field names select parts of a record.

Position and field name answer different questions

An index identifies a position in an ordered collection. JavaScript array positions begin at zero, so index 0 is the first item and index 1 is the second. An index tells you where an item is, not its permanent school identifier.

A field, called a property on a JavaScript object, gives one piece of information a name. The field science means a science mark regardless of where the student’s record sits in an array.

Review numbers, strings and missing values and assignment first. Each name and mark below uses those basic values; the collection supplies a structure for grouping them.

Read a student, a mark and the class size

This complete example uses JavaScript. Square brackets create an array. Each pair of braces creates an object record, with a colon between a field name and its value. Commas separate fields and array items.

students[0] selects Maya’s record, and .name reads its name. students[1].maths selects Ravi’s maths mark. .length reports how many items are in this array. Predict the three output lines before comparing them with the displayed result.

JavaScript · local teaching example

const students = [
  { name: 'Maya', maths: 40, science: 35 },
  { name: 'Ravi', maths: 28, science: 42 }
];
console.log(students[0].name);
console.log(students[1].maths);
console.log(students.length);

Expected output

Maya
28
2

Update one field in a student record

Here is one student record in JavaScript. The assignment student.science = 40 changes that field. It does not replace the student’s name or maths mark. The total now uses 40 + 40 and becomes 80.

The name student was declared with const, which prevents assigning a different object to that name. It does not make the object’s fields unchangeable. A record is a data arrangement; you do not need a class just to group these fields.

JavaScript · local teaching example

const student = { name: 'Maya', maths: 40, science: 35 };
student.science = 40;
console.log(student.name);
console.log(student.maths + student.science);

Expected output

Maya
80

Add a student to an ordered list

This JavaScript array holds names rather than whole records. push is an operation supplied by arrays; it appends an item. The array’s length changes from 2 to 3, and the new name is at index 2.

An array can grow, and removing or reordering entries can change their positions. Do not use an index as though it were a permanent student ID. The array documentation describes these operations; the dedicated arrays lesson develops them further.

JavaScript · local teaching example

const names = ['Maya', 'Ravi'];
console.log(names.length);
names.push('Asha');
console.log(names.length);
console.log(names[2]);

Expected output

2
3
Asha

Read a collection inside a record

In this JavaScript version, each record’s marks field contains an array. To read students[1].marks[0], first select Ravi, then his marks array, then its first value: 28.

This representation needs a convention: position 0 means maths and position 1 means science. Named fields such as maths and science are often clearer for subjects, while arrays are useful for a varying list of results. Nest data because the task has that relationship, not just because nesting is possible.

JavaScript · local teaching example

const students = [
  { name: 'Maya', marks: [40, 35] },
  { name: 'Ravi', marks: [28, 42] }
];
console.log(students[1].name);
console.log(students[1].marks[0]);

Expected output

Ravi
28

Two names can refer to the same record

In this JavaScript example, assigning student to teacherView does not create an independent student record. Both names refer to the same object. Changing its science field through teacherView is visible through student.

This differs from copying a primitive numeric mark. Both output lines are 40 because there is one shared record. If you require an independent copy, create one deliberately and consider any nested records as well. The object guide explains object identity and properties.

JavaScript · local teaching example

const student = { name: 'Maya', science: 35 };
const teacherView = student;
teacherView.science = 40;
console.log(student.science);
console.log(teacherView.science);

Expected output

40
40

One object, two ways to reach it

  1. Create the record

    student refers to Maya’s record.

    science: 35
  2. Share the reference

    teacherView refers to that same record.

    student → record ← teacherView
  3. Edit the shared record

    Update science through teacherView; both reads see the change.

    science: 40 for both names
These cards track the same shared object over time; they do not show two copies.

Choose uniqueness or lookup when order is not the main need

A class list needs order, but a list of students who attended should sometimes count each identifier only once. A set represents unique values. In this JavaScript example, repeated identifier 007 occupies one set entry, so .size is 2.

A map associates keys with values. Here each inner pair supplies an identifier and a maths mark. get retrieves the mark for identifier 009 without making you guess an array position. The new expressions create these built-in collections.

Arrays, sets and maps answer different questions: what is the ordered list, which values are present, and which value belongs to a key? Continue later with sets and dictionaries and keyed lookup. Languages use different names and implementations for these structures.

JavaScript · local teaching example

const attended = new Set(['007', '007', '009']);
console.log(attended.size);
const mathsById = new Map([['007', 40], ['009', 28]]);
console.log(mathsById.get('009'));

Expected output

2
28

Correct only Ravi’s science mark

JavaScript · original class records for this task

const students = [
  { name: 'Maya', maths: 40, science: 35 },
  { name: 'Ravi', maths: 28, science: 42 }
];
console.log(students[0].name);
console.log(students[1].maths);
console.log(students.length);

The starting class array is reproduced above. Change Ravi’s science mark from 42 to 45, then display his name and total. Also display Maya’s science mark to check that you changed the intended record.

Worked answer

Ravi is the second record, so select index 1. Change its science field, then add his maths mark 28 to science 45. The total is 73; Maya’s science mark remains 35. The complete JavaScript answer follows.

JavaScript · local teaching example

const students = [
  { name: 'Maya', maths: 40, science: 35 },
  { name: 'Ravi', maths: 28, science: 42 }
];
students[1].science = 45;
console.log(students[1].name);
console.log(students[1].maths + students[1].science);
console.log(students[0].science);

Expected output

Ravi
73
35

A missing record is different from a missing field

JavaScript · which positions and fields exist?

const students = [
  { name: 'Maya', maths: 40, science: 35 },
  { name: 'Ravi', maths: 28, science: 42 }
];
console.log(students[0].name);
console.log(students[1].maths);
console.log(students.length);

This array contains only two records. students[2] is undefined because index 2 is beyond its populated entries. Trying to read students[2].name would then try to access a property on undefined and produce an error.

Maya’s record exists, but it has no field named grade. Reading students[0].grade gives undefined. These cases need different checks: first confirm the selected record exists, then check the fields the task requires.

Using students[1] when you meant the first student is another mistake: it can return a perfectly valid record, but the wrong one. Good tests include empty lists, missing fields and more than one student. Later, choosing a data structure compares these decisions in more detail.

Check: select the second student’s science mark

JavaScript · class array for this question

const students = [
  { name: 'Maya', maths: 40, science: 35 },
  { name: 'Ravi', maths: 28, science: 42 }
];
console.log(students[0].name);
console.log(students[1].maths);
console.log(students.length);
Which expression reads Ravi’s science mark in this class array?
Choose one answer. You can try again.

Answer and explanation

students[1].science

Yes. Index 1 selects Ravi’s record and .science selects his science field.

Check: what did the assignment copy?

JavaScript · shared record for this question

const student = { name: 'Maya', science: 35 };
const teacherView = student;
teacherView.science = 40;
console.log(student.science);
console.log(teacherView.science);
After this program finishes, why does student.science print 40?
Choose one answer. You can try again.

Answer and explanation

teacherView and student refer to the same object, whose field was changed.

Yes. Both names read the updated science field from the shared record.

Let the structure match the relationship

Use a record for related named fields, an array for an ordered collection, a set for unique values and a map for key-based lookup. Collections can contain records and other collections.

Trace a nested expression one selection at a time. Positions begin at zero in JavaScript arrays, missing records need handling, and assigning an object to another name does not make an independent copy.

Explain why correcting Ravi uses index 1 and why a teacher’s shared view can change the same student record. Next, learn how type rules help identify incompatible values.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Type Systems: Static and Dynamic Typing →