Enginekeycatobject1const cat = { name: "Mochi", age: 3 };2console.log(cat.name);3cat.age = 4;4cat.color = "grey";5const key = "color";6console.log(cat[key]);7delete cat.age;8console.log(cat.age);9console.log(cat);ConsoleMochicat.js:2greycat.js:6undefinedcat.js:8{ name: 'Mochi', color: 'grey' }name"Mochi"age3color"grey""Mochi"4"color""color""grey"undefined{…}
Back: the engine, the variables key and cat, and the object cat refers to (a tray of properties)Front: the script cat.js and the consoleTokens: instructions and the values moving in and out of the objectAt the back, the JavaScript engine, two variables (key and cat) and the object cat refers to: a tray of properties, each a key and a value. In front, the script cat.js and the console. Line 1 builds the object; dots and brackets read a property; assigning changes one or adds a new one; delete removes one; a missing key reads as undefined. The tray of named slots is a picture, not how an engine stores an object. Engines such as V8 use hidden shapes and fast lookups, but the result is the same as the picture: a set of key and value pairs.

Making an object

  1. An object keeps related values together under names. Curly braces make one: { name: "Mochi", age: 3 }. The engine builds the object in memory, and the variable cat refers to it.

Reading and changing

Keys in variables

Removing a property

What happens

// step 1/10 · object-literal
line 1   const cat = { name: "Mochi", age: 3 };
{ ... }  an object literal: build a new object
engine   makes it with two properties
cat      refers to it (it holds a reference)
cat      { name: "Mochi", age: 3 }
key      (not made yet)
console  (empty)

cat does not contain the object; it holds a reference to it. Two variables can refer to the same object, and a change made through one shows through the other. That is why const cat still lets you change, add and delete properties. The tray of named slots is a picture, not how an engine stores an object. Engines such as V8 use hidden shapes and fast lookups, but the result is the same as the picture: a set of key and value pairs.