Skip to content

Commit

Permalink
Update optional-chaining.md
Browse files Browse the repository at this point in the history
minor fixes
  • Loading branch information
mamtawardhani authored Dec 27, 2024
1 parent 9c5c47e commit 4682e97
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions content/javascript/concepts/optional-chaining/optional-chaining.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Optional Chaining'
Description: 'The optional chaining operator simplifies the comparison of multiple data properties in a chain of connected objects.'
Description: 'The optional chaining operator allows safe access to nested object properties or methods without having to explicitly check if intermediate properties exist.'
Subjects:
- 'Web Development'
- 'Computer Science'
Expand All @@ -12,7 +12,7 @@ CatalogContent:
- 'paths/front-end-engineer-career-path'
---

The **optional chaining** operator simplifies comparing multiple data properties in a chain of connected objects. This is especially valuable if any of the properties are `null`, as the operator will return `undefined` instead of throwing an error.
The **optional chaining** (**`?.`**) operator simplifies comparing multiple data properties in a chain of connected objects. This is especially valuable if any of the properties are `null`, as the operator will return `undefined` instead of throwing an error.

Optional chaning can be used to validate nested object properties, call optional functions or methods on optional objects, accessing array elements, and more.

Expand All @@ -22,7 +22,7 @@ The primary benefits of using optional chaining instead of regular syntax includ

The basic syntax for using optional chaining is as follows:

```js
```pseudo
// To access an object property
object?.property;
Expand Down Expand Up @@ -124,16 +124,20 @@ console.log(`${phrase}`);
//This can be rewritten as:
const phrase = person?.commonPhrase?.();
console.log(`${phrase}`);
```
The output of the above code will be as follows:
//The output for both is:
```shell
Alice always says "I love Oklahoma."
Alice always says "I love Oklahoma."
```
## Codebyte Example
Run the following example and compare the regular syntax of conditional statements and optional chaining on a more complex data set.
Run the following example and compare the regular syntax of conditional statements and optional chaining on a more complex data set:
```codebyte/js
```codebyte/javascript
const people = [
{
name: "Blake",
Expand Down

0 comments on commit 4682e97

Please sign in to comment.