-1

Do you know the expression ? when using it like this:

data?.name

I mean what does this question mark (?) in-between the data and the point do?

  • See [What does this symbol mean in JavaScript?](/q/9549780/4642212) and the documentation on MDN about [expressions and operators](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators). – Sebastian Simon Dec 10 '22 at 15:23

2 Answers2

2

This is called "Optional chaining". Say you make an axios request and expect the response.data object. You don't KNOW if data will be there, but you expect it to. The question mark is a shorthand for writing data && data.name If data is undefined, the data.name code does not run and thus you don't get an error (cannot access name of undefined)

1

it is called Optional chaining (?.), and basically it will return unidentified instead of throwing an error

you can read more about it here

https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Operators/Optional_chaining