Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 09 / 34

Arrays & objects

01

Understand the idea

A product list suddenly appears in a different order after another screen sorts it. Both screens were holding the same .

Choose how to find a value

A task has a title and a completion state, so an object fits it. A day's tasks have an order, so an array fits the collection. You can combine the two instead of inventing separate like taskOneTitle and taskTwoTitle.

Reading names and positions

Object properties are read by name. Dot notation works for a known identifier; brackets let a variable supply the key. A missing property reads as . Reading another property from that missing value is what usually throws, so check the parent when data is optional.

Array positions start at zero: the first task is at index 0. length counts items, so the last position of a non-empty array is length minus one. Dot notation uses a written property name; bracket notation is useful when the name comes from another variable. Reading a property that is not there gives undefined.

Working with named and ordered data
ToolUse it forWatch for
Object propertyFind a named field such as task.titleA missing field reads as undefined
Array indexFind an item by position such as tasks[0]The first index is 0, not 1
push / popAdd or remove at the endBoth change the existing array
Object or array spreadCreate a shallow outer copyNested objects stay shared
DestructuringGive selected fields or items local namesIt does not deep-copy the values
Array.isArray(value)Check specifically for an arraytypeof an array is "object"

A copy may still share data

Arrays use zero-based positions and have a length. The last valid index is one less than that length. Destructuring can give a few positions or fields useful local names, while a default handles an undefined value; it does not replace .

Updating without surprises

Some the collection. push, splice, and sort change an existing array. slice and spread make shallow copies. Shallow means nested objects are still shared, so copying the array alone does not make each record independent.

Follow the two results

When you need a new order without changing the original array, copy before sorting. For numbers, give sort a numeric comparator; its default compares string representations. When changing one record, copying that record too makes the intended boundary of the change visible.

The example uses map to produce an updated array. For each task, the object spread copies its fields into a new outer object, then done replaces that field. The original task stays unfinished. You will study map in more detail later; here, watch which object changes and which stays independent.

02

Mental model

FOLLOW THE VALUE

tasks
one record
done: false

The initial array contains an object reference.

01 / 03Trace the transformation

03

Live example

map creates a new here, and spread creates a new record inside it. Both copies matter for keeping the original task unchanged.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Keep the original record unchanged

Return a new task object with done set to true, leaving tasks[0].done false.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

false
true
1
Review

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does array spread copy?
    02What is the last valid index of an array of length 3?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson