Skip to lesson content
Web Technologies โ€บ Level 15
LEVEL 15 ยท JAVASCRIPT

JavaScript Arrays, Objects and Logic

Model collections, preserve source data and explain every transformation.

๐Ÿ“š Arrays๐Ÿ—ƒ๏ธ Objects๐Ÿ”— Data logic๐Ÿงช Lab + MCQs
01

Learning Objectives

Collections

Use array positions and iteration deliberately.

Records

Give related properties meaningful names.

Transformations

Distinguish map, filter, find and reduce.

Verification

Check source preservation, no-match results and tie behavior.

02

Model the Task Before Choosing a Method

One catalog, three levels
Array

The ordered collection of workshops.

Object

One workshop record with named fields.

Property

A value such as title, capacity or reserved.

03

Arrays โ€” Positions, Length and Access

const topics = ["HTML", "CSS", "JavaScript"];
console.log(topics[0]); // HTML
console.log(topics.length); // 3
console.log(topics[3]); // undefined
console.log(Array.isArray(topics)); // true
04

Changing Arrays โ€” Know the Mutation

const topics = ["HTML", "CSS"];
const length = topics.push("JavaScript"); // 3
const preview = topics.slice(0, 2); // ["HTML", "CSS"]
const removed = topics.splice(1, 1); // ["CSS"]
// topics is now ["HTML", "JavaScript"]
05

Iteration โ€” Indexes or Values?

const topics = ["HTML", "CSS", "JavaScript"];
for (let index = 0; index < topics.length; index += 1) {
  console.log(index, topics[index]);
}
for (const topic of topics) {
  console.log(topic);
}
06

Object Literals and Named Properties

const workshop = {
  id: "w1", title: "HTML", capacity: 30, reserved: 12
};
console.log(workshop.title); // HTML
const key = "capacity";
console.log(workshop[key]); // 30
console.log(workshop.missing); // undefined
07

Object Updates, Methods and Inspection

function remainingSeats(workshop) {
  return workshop.capacity - workshop.reserved;
}
const workshop = { id: "w1", title: "HTML", capacity: 30, reserved: 12 };
console.log(remainingSeats(workshop)); // 18
console.log(Object.keys(workshop));
// ["id", "title", "capacity", "reserved"]
08

References, Identity and Shallow Copies

const original = [{ title: "HTML", meta: { room: "A" } }];
const outerCopy = [...original];
outerCopy[0].title = "Updated"; // changes original[0].title too

const recordCopy = { ...original[0] };
recordCopy.title = "Local"; // independent top-level field
recordCopy.meta.room = "B"; // nested meta remains shared

A new outer container and a new nested object are different claims. Explain exactly which levels are copied.

09

Destructuring, Spread and Defaults

const workshop = { title: "HTML", capacity: 30, reserved: 12 };
const { title, capacity, reserved = 0 } = workshop;
const enriched = { ...workshop, remaining: capacity - reserved };
const [first, second] = ["HTML", "CSS"];
10

map โ€” Build a Derived Collection

const enriched = workshops.map(workshop => ({
  ...workshop,
  remaining: workshop.capacity - workshop.reserved
}));
11

filter and find โ€” Collection or First Match?

const available = enriched.filter(workshop => workshop.remaining > 0);
const chosen = enriched.find(workshop => workshop.id === "w2");
if (chosen === undefined) {
  console.log("Workshop not found");
}
12

some and every โ€” Ask a Collection Question

const anyAvailable = enriched.some(w => w.remaining > 0);
const allFull = enriched.every(w => w.remaining === 0);
const nonEmptyAndFull = enriched.length > 0 && allFull;
13

reduce โ€” Summarize with an Initial Value

const totalRemaining = enriched.reduce(
  (total, workshop) => total + workshop.remaining,
  0
);
// [] with this initial value gives 0
14

Sorting โ€” Comparator, Mutation and Ties

const numbers = [2, 10, 3];
[...numbers].sort(); // [10, 2, 3]
[...numbers].sort((a, b) => a - b); // [2, 3, 10]

const bySeats = [...enriched].sort((a, b) =>
  b.remaining - a.remaining || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)
);
15

A Clear Pipeline โ€” Derive, Select, Order, Summarize

Catalog transformation
map

Derive a remaining field on new flat records.

filter

Select by title and availability.

copy + sort

Order a separate result array.

reduce

Total the selected remaining counts.

16

Premium Visualizer โ€” Workshop Collection Journey

Follow the normal dataset through a fixed available-workshop query. Use the established numbered timeline and playback controls to inspect the role of each method. The source data stays separate from the displayed results.

JAVASCRIPT COLLECTION TRACE
Step 1 of 5
STEP 01

Start with four workshop records

The fixed catalog contains HTML, CSS, JavaScript and Accessibility.

capacity / reserved: 30/12, 20/20, 40/10, 12/0

What is happening?

The predefined records meet the data contract. Preserve their original values and source order.

17

Premium Interactive โ€” Catalog Logic Lab

Ready to select available workshops.

Original source records

Operations applied

Selected records and summary

18

Debugging โ€” Inspect Shape, Return and Mutation

Shape

Confirm an actual array and the required record fields.

Return

Check that callbacks return their mapped value or predicate decision.

Mutation

Inspect both the outer array and referenced objects.

Empty case

Check length, find results and reduction initial values.

19

Interview Questions โ€” Flip to Explain

QUESTION

What is the difference between an array and a record object?

Click or press Enter to explain
ANSWER

An array organizes an indexed collection; an object groups named properties. An array of objects can represent a catalog.

QUESTION

Does const prevent push on an array?

Click or press Enter to explain
ANSWER

No. Const prevents reassignment of the binding, not allowed changes to the array it references.

QUESTION

How do slice and splice differ?

Click or press Enter to explain
ANSWER

Slice creates a shallow range copy. Splice changes its receiver and returns the removed items.

QUESTION

What is the difference between map and filter?

Click or press Enter to explain
ANSWER

Map derives output values for present input elements. Filter selects existing elements whose predicate passes.

QUESTION

What does find return with no match?

Click or press Enter to explain
ANSWER

Undefined. Check that result before accessing properties.

QUESTION

Why does every return true for an empty array?

Click or press Enter to explain
ANSWER

There is no present item that fails the predicate. Combine it with a non-empty check when the task requires records to exist.

QUESTION

Why copy before calling sort?

Click or press Enter to explain
ANSWER

Sort mutates its receiver. Sorting a copied outer array preserves the original array order, while item references may still be shared.

QUESTION

Why provide an initial value to reduce?

Click or press Enter to explain
ANSWER

It defines the accumulatorโ€™s starting value and supports an empty collection. For seat totals, initial zero gives an empty total of zero.

20

MCQ Practice โ€” Explain the Collection

PRACTICE

1. What is the first ordinary array index?

PRACTICE

2. What does push return?

PRACTICE

3. Which method creates a shallow range copy without removing source items?

PRACTICE

4. Which loop directly visits array values?

PRACTICE

5. What does map produce when this dense input callback returns a derived record?

PRACTICE

6. What does find return when no record matches?

PRACTICE

7. What does an empty arrayโ€™s every predicate test return?

PRACTICE

8. Which initial value fits a reduce calculation of total remaining seats?

PRACTICE

9. What does default sort do to [2, 10, 3]?

PRACTICE

10. Does [...records] deeply clone every nested record?

PRACTICE

11. What should a no-match filtered result be reported as?

PRACTICE

12. Which total belongs to normal/available in this lesson?

21

Extra Practice โ€” Test Specific Collection Cases

Normal query

Select normal/available/most first. Expected titles: JavaScript, HTML, Accessibility; total 60.

Empty source

Select empty/all. Expected: zero records, total zero, some available false, raw every full true.

No availability

Select full/available. Expected: no matching records; do not read a nonexistent first title.

Search phrase

Use normal/all and search HTML. Expected: one HTML record, remaining total 18.

No match

Search a phrase absent from every title. Expected: an empty selection and clear explanation.

Tie rule

Select ties/available/most first. Expected: IDs t1, t2, t3; each has 10 remaining, total 30.

Copy experiment

  • Copy an array containing one object, then edit the copied item title.
  • Expected: shared item changes the original until you copy that object too.

Reduce comparison

Total [18, 0, 30, 12] with a loop and reduce initial zero. Expected: both yield 60; empty input yields zero.

22

Quick Revision

Array: Use zero-based positions and distinguish length from an ID.
Object: Group named fields and preserve stable identity.
Mutation: Know both the return value and the changed receiver.
Copies: New outer arrays can share referenced items.
map: Return a derived value for each present item.
filter/find: Distinguish a selected array from a first match.
some/every: Explain the empty-case boolean results.
reduce: Choose an initial accumulator that fits the task.
sort: Copy first, use a comparator and state ties.
Pipeline: Summarize the selection actually displayed.
23

Glossary โ€” Flip to Learn

TERM

Array

Click to see meaning
DEFINITION

Array

An object used for indexed collections and array operations.

TERM

Index

Click to see meaning
DEFINITION

Index

A position key used to access an array item, commonly starting at zero.

TERM

Property

Click to see meaning
DEFINITION

Property

A named or keyed field on an object.

TERM

Reference

Click to see meaning
DEFINITION

Reference

An association with an object that can be shared by multiple bindings.

TERM

Mutation

Click to see meaning
DEFINITION

Mutation

A change to an existing object or array.

TERM

Shallow copy

Click to see meaning
DEFINITION

Shallow copy

A new outer container whose nested object references may remain shared.

TERM

Callback

Click to see meaning
DEFINITION

Callback

A function passed to another operation to be called by it.

TERM

Predicate

Click to see meaning
DEFINITION

Predicate

A function or expression used to decide whether a condition passes.

TERM

Accumulator

Click to see meaning
DEFINITION

Accumulator

The carried summary value updated during a reduction.

TERM

Comparator

Click to see meaning
DEFINITION

Comparator

A function that defines how two values should be ordered for sorting.

24

Final Challenge โ€” A Catalog Query Function

25

Level 15 Complete?

Before marking complete, derive and select a catalog, sort a copy, summarize an empty collection and explain a shallow-copy limitation. Completion is a local study marker rather than an assessment score.