Syntax I've never seen

Hello I am working on a React weather project and I’ve come across syntax that I’ve never seen before can anyone help it looks like it might be a variation of the ternary operator? If you can could you also link some links to MDN article explaining this syntax many thanks.

if (!response?.woeid) return;

I am getting a object response from an API that has a property called woeid but I don’t understand this syntax. can you explain what this line of code is saying thanks so much

This is called an optional chaining.

1 Like

You can read this article about it

1 Like

MDN:

Edit: The very short version is it returns undefined instead of blowing up.

let obj = { name: 'John' }
obj.name
// 'John'
obj = null
obj.name
// Uncaught TypeError: Cannot read properties of null (reading 'name')
obj?.name
// undefined
1 Like

It is relatively new in JS so it is not covered in FCC curriculum or many other JS tutorials. But it is really cool.

1 Like

Thank you your reply was very helpful.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.