Skip to content

Commit 7cbb265

Browse files
authored
optional chaining
1 parent 622978b commit 7cbb265

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,25 @@ console.log(age);
390390
// expected output: "0"
391391
```
392392
393+
# Optional chaining
394+
```javascript
395+
396+
const car = {}
397+
const carColor = car.name.color
398+
console.log(carColor);
399+
// error- "Uncaught TypeError: Cannot read property 'carColor' of undefined
400+
401+
//In JavaScript, you can first check if an object exists, and then try to get one of its properties, like this:
402+
const carColor = car && car.name && car.name.color;
403+
console.log(carColor);
404+
//undefined- no error
405+
406+
407+
//Now this new optional chaining operator will let us be even more fancy:
408+
409+
const newCarColor = car?.name?.color;
410+
console.log(newCarColor)
411+
//undefined- no error
412+
413+
//You can use this syntax today using @babel/plugin-proposal-optional-chaining
414+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy