---
title: "Discussion on \"How to use the Javascript Optional Chaining operator (?.)\" | Hashnode"
description: "Discussion on \"How to use the Javascript Optional Chaining operator (?.)\". Ever encountered an error of the sort?\nCannot read property 'colour' of undefined\nIn this case, you probably expected an object, yourObjectName to have a colour property. So you went ahead to make use of it this way:\nif (yourObjectName.colour === 'bl..."
url: "https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to use the Javascript Optional Chaining operator (?.)](https://blog.lawrecks.com/how-to-use-the-javascript-optional-chaining-operator?utm_source=hashnode&utm_medium=feed)

Ever encountered an error of the sort? Cannot read property 'colour' of undefined In this case, you probably expected an object, yourObjectName to have a colour property. So you went ahead to make use of it this way: if (yourObjectName.colour === 'bl...

Aug 13, 2021 · 2 min read · blog.lawrecks.com  
Updated Aug 23, 2021  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#first-post-1](https://hashnode.com/tag/first-post-1)

## Written by

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

@lawrecks

  
[View profile](https://hashnode.com/@lawrecks)

Software Engineer

## Responses 8

Markdown supported · ⌘↵ to post

[Sanjeev Vishwakarma](https://hashnode.com/@Sanjeev92)

[Aug 28, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/612a8605fd0bf8636d76c4c4)

Thanks for sharing found it very useful .

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Sep 5, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/612a8605fd0bf8636d76c4c4#reply-61341315489de51a6a7aea3d)

I'm glad you did

[Micheal Adisa](https://hashnode.com/@michellead)

[Aug 16, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611abb8f888ca15f5d38f95c)

Great read man! Weldone.

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 16, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611abb8f888ca15f5d38f95c#reply-611ad3c9888ca15f5d38fd74)

Thanks, man

[joshua torgbor Obodai](https://hashnode.com/@miyachi)

[Aug 16, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611a4986ac5c985f36438566)

Well done. A good read bro!

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 16, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611a4986ac5c985f36438566#reply-611a7411888ca15f5d38ea90)

Thanks bro!

[Jade](https://hashnode.com/@hjades)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/6117d6ace9f08a51fd85fdf3)

Nice article! Really like this approach 🤩

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/6117d6ace9f08a51fd85fdf3#reply-611801bee9f08a51fd8603e0)

Thank you

[Favourite Jome](https://hashnode.com/@Favourite)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611799aff6ebde51f64fa4b4)

Welcome to Hashnode, I've been using optional chaining at my work place without knowing what's it called 😲, I should have asked questions...

Nicely written and explained!

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611799aff6ebde51f64fa4b4#reply-611801b6f6ebde51f64fb06f)

Thank you

[Success Ologunsua](https://hashnode.com/@TheSussex)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611749aaf6ebde51f64f9f56)

Thanks for this tip, nice article👏

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/611749aaf6ebde51f64f9f56#reply-6118012be9f08a51fd8603c7)

Thank you!

[Chibuikem Nick](https://hashnode.com/@chibuikem-nick)

[Aug 13, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/6117001c94173a4decbb9807)

Nice article. 👍

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 14, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/6117001c94173a4decbb9807#reply-61171a75f6ebde51f64f9cc7)

Thank you 🙌🏾

[Moshood Sikiru](https://hashnode.com/@ayolan)

[Aug 13, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/61169d9916b6034ad04b0530)

This would still throw **_cannot read property color of undefined_** if **_yourObjectName_** does not exist:

```
if (yourObjectName.color && yourObjectName.color === 'blue') {
  return 'Blue is cool';
}
```

**Perhaps you meant:**

```
if (yourObjectName && yourObjectName.color === 'blue') {
  return 'Blue is cool';
}
```

[Ugochukwu Ejiogu](https://hashnode.com/@lawrecks)

[Aug 13, 2021](https://hashnode.com/posts/how-to-use-the-javascript-optional-chaining-operator/611698a81667fc4aab5dd09b/comment/61169d9916b6034ad04b0530#reply-6116d4cc94173a4decbb9655)

That's true. Thanks for the observation!
