JavaScript Objects
Learn the labeled container: property access and destructuring, methods, spread and shorthand, and the optional chaining that survives missing data.
Basic
An object in JavaScript is a collection of named values, written in braces as key: value pairs, and it is the shape nearly all real data takes: one object per order, per user, per menu item, arrays of them for the collections. Python calls the same idea a dictionary; JavaScript goes further and lets objects carry behavior as well as data, functions stored as properties, called methods.
Reading and writing properties
|
|
Two readers, one writer, no declarations needed.
Dot notation is the default and reads best; brackets earn their keep when the key lives in a variable or arrives as text,
loaf[field]. Reading a missing property returns
undefined rather than raising, forgiving and dangerous in exactly the way arrays are, and the cure is the same: test explicitly, or reach for the chaining tools below.
Destructuring and the spread
Pulling several values out of an object is so common that the language grew a syntax for it: destructuring declares variables named after the properties, in place, with defaults allowed. Its partner, spread, expands an object's properties into a new one, which is how objects get copied and updated without being mutated, the idiom behind nearly every state update in modern code.
|
|
Reading several values at once, and updating without mutating.
Methods: behavior as a property
A property whose value is a function is a method, callable with dot notation, and inside a regular method the
this keyword refers to the object the method was called on. That last sentence carries the famous complexity of
this, and the practical summary for everyday code is short: define methods with the shorthand syntax, call them as
obj.method(), and
this is the object you expect. Arrow functions do not bind their own
this, which makes them wrong as object methods and right inside callbacks, one distinction worth carrying forward.
|
|
The shorthand method syntax, and this meaning the object.
Optional chaining, for data that might be absent
Real data has holes: an order without a customer, a customer without an address. Chained property access through a hole throws, and the old guard was a trench of
&& checks. Optional chaining,
?., short-circuits the whole chain to
undefined at the first hole, and pairs with
?? for a default, turning five defensive lines into one readable one.
|
|
One question mark per hole, one readable line.
Run the order book
The example below keeps the day's orders as an array of objects and reports on them with the lesson's tools, printing to the preview console. Run it, then work the edges: destructure
first with a
discount = 0 default, add a method
summary() to an order and call it, and delete one customer entirely to watch optional chaining keep the report alive where plain access would have thrown.
Output
Ready to run.
What to notice
Spread gave updated copies while the originals held still, which is how larger programs stay predictable.
Optional chaining with
??described missing data in one line per field.The method found its total through
this.orders, the object it was called on.
Quick check
If you can destructure with defaults, update an object without mutating it, and read through missing data without a guard trench, labeled data is yours to command. Next, the last stop: loops. When you are ready, take the JavaScript Objects quiz.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.