> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/optional-chaining.md).

# Optional Chaining

In JavaScript, optional chaining is a feature that allows you to access an object's properties or methods without having to check if the object is `null` or `undefined`. It allows you to safely access a chain of properties that may not exist, without having to check for the existence of each one.

For example, suppose you have an object with several nested properties, and you want to access the value of a property that is several levels deep. Without optional chaining, you would have to check whether each object in the chain is `null` or `undefined` before accessing the next one, like this:

```javascript
let obj = {
  a: {
    b: {
      c: 'hello'
    }
  }
};

let value;
if (obj && obj.a && obj.a.b && obj.a.b.c) {
  value = obj.a.b.c;
}

console.log(value); // 'hello'
```

With optional chaining, you can simplify this process by using the `?.` operator to access the properties in the chain. If any property in the chain is `null` or `undefined`, the expression will return `undefined` instead of throwing an error:

```javascript
let value = obj?.a?.b?.c;

console.log(value); // 'hello'
```

Optional chaining can also be used to call methods on an object, like this:

```javascript
let obj = {
  a: {
    b: {
      c: () => 'hello'
    }
  }
};

let value = obj?.a?.b?.c();

console.log(value); // 'hello'
```

In summary, optional chaining is a useful feature in JavaScript that allows you to safely access nested properties or methods of an object without having to manually check for `null` or `undefined` values.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://demirels-organization.gitbook.io/javascript-tutorial/optional-chaining.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
