

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Objects
incomplete
2: No Colon
incomplete
3: Updating Properties
incomplete
4: Nesting Properties
incomplete
5: Nesting Properties Quiz
incomplete
6: Optional Chaining
incomplete
7: When to Chain
incomplete
8: Object Methods
incomplete
9: Methods Mutate
incomplete
10: Initializing Props
incomplete
11: Strings As Keys
incomplete
12: This
incomplete
13: Arrow Functions
incomplete
14: Fat Arrows and This
incomplete
15: Spread Syntax
incomplete
16: Return Objects
incomplete
17: Destructuring
incomplete
18: Not Bound
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Nested data can quickly become hard to work with. In most production systems you'll deal with 3-4 levels of object nesting on a regular basis.
When using the normal . operator, if the object on the left side of the . is null or undefined, you'll get a TypeError at runtime! Thankfully, JavaScript has recently added a new operator to make dealing with this headache easier, the optional chaining operator: ?.
const tournament = {
prize: {
units: "dollars",
value: 100,
},
};
const h = tournament.referee.height;
// TypeError: Cannot read properties of undefined (reading 'height')
So, if you're not sure whether the referee property exists (maybe it was sent to us over the network) we can use the optional chaining operator to avoid the error:
const tournament = {
prize: {
units: "dollars",
value: 100,
},
};
const h = tournament.referee?.height;
// h is simply undefined, no error is thrown
Our Textio database is pretty smart - it will only save a value if the value is "truthy" in JavaScript. If it's "falsy", it will just ignore it without errors.
You should be able to do this in 1 line with the optional chaining operator.