Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

Objects

Object 12 minObjects group named properties, and variables hold references to them, so assigning an object shares it instead of copying it.

Objects

In short: an object bundles named properties into one value — everything that isn't one of the six primitives (string, number, boolean, undefined, null, bigint, symbol) is an object, including arrays and functions. The rule that trips people up: primitives are copied, objects are shared. A variable holding a primitive holds the value itself; a variable holding an object holds a reference — an arrow to the object — so assigning it doesn't make a second object, it makes a second arrow to the same one.

Two name tags, song and copy, both with arrows pointing to the same box labelled plays: 0 — changing through copy changes the one shared box, while a dashed note below shows a primitive assignment copying the value itself, not an arrow

Two names for one song

Handing an object to a second variable doesn't clone it:

javascript

song and copy point at the same object, so a change made through either name is visible through both, and === compares the arrows, which are equal. Compare that with a primitive:

javascript

b = a copies the number itself, so b and a are independent from that point on. Objects never work this way.

Two names, one songStep 1 of 5
  1. Create the object

    const song = { plays: 0 }: one object box, and song holds an arrow to it.

Reading a key that doesn't exist

Dot notation only ever looks for a property with that literal name:

javascript

song[key] reads the value stored in the variable key and uses it as the property name — "title" — so it finds "Blue Hour". song.key looks for a property that is spelled k-e-y, which this object doesn't have, so it gives undefined. Use brackets whenever the key lives in a variable.

Two "equal" objects that aren't ===

A cache that checks "have I seen this object before?" with === gets surprised by data that merely looks the same:

javascript

{ plays: 5 } === { plays: 5 } is false — two separate literals make two separate boxes, even with identical contents; === on objects only ever checks "is this the same box", never "do the contents match". To actually get an independent copy, build a new object on purpose with { ...song }.

Quick practice

quick drill

What does const s = { plays: 1 }; const t = s; t.plays = 2; console.log(s.plays); log?

quick drill

What does console.log({ title: "A" } === { title: "A" }) log?

quick drill

With const song = { title: "Blue Hour" };, what does song["title"] give?

quick drill

Which of these is NOT an object in JavaScript?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy did both change?

A developer writes const edited = song; edited.title = "Blue Hour (Live)"; and is surprised that song.title changed too. Explain why, and how to make a separate copy.