MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/node/comments/jrau9t/conditionally_call_a_function_with_optional/gbsxdl7/?context=3
r/node • u/wobsoriano • Nov 10 '20
126 comments sorted by
View all comments
1
Can someone help ELI5 for a jr developer?
4 u/wobsoriano Nov 10 '20 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chainingThe optional chaining ‘?.’ is an error-proof way to access nested object properties, even if an intermediate property doesn’t exist.const user = {}; // error: Uncaught TypeError: Cannot read property 'first' of undefined console.log(user.name.first) // ok, but returns undefined console.log(user?.name?.first) 2 u/FunkyFreshFarming Nov 10 '20 Okay that helps me understand how it works, thank you for the link. I still don't understand why we would want to use this however. 1 u/Not_Sugden Nov 10 '20 its mainly just clearer for specific use cases, but idk i dont find it all that useful, i've only used it like once
4
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
The optional chaining ‘?.’ is an error-proof way to access nested object properties, even if an intermediate property doesn’t exist.
const user = {}; // error: Uncaught TypeError: Cannot read property 'first' of undefined console.log(user.name.first) // ok, but returns undefined console.log(user?.name?.first)
2 u/FunkyFreshFarming Nov 10 '20 Okay that helps me understand how it works, thank you for the link. I still don't understand why we would want to use this however. 1 u/Not_Sugden Nov 10 '20 its mainly just clearer for specific use cases, but idk i dont find it all that useful, i've only used it like once
2
Okay that helps me understand how it works, thank you for the link. I still don't understand why we would want to use this however.
1 u/Not_Sugden Nov 10 '20 its mainly just clearer for specific use cases, but idk i dont find it all that useful, i've only used it like once
its mainly just clearer for specific use cases, but idk i dont find it all that useful, i've only used it like once
1
u/FunkyFreshFarming Nov 10 '20
Can someone help ELI5 for a jr developer?