Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • \$\begingroup\$ Wow, TIL about the ?.() operator \$\endgroup\$ Commented Jan 6, 2022 at 16:53
  • 1
    \$\begingroup\$ @pxeger The () isn't really part of the operation. .? is the 'optional chaining' operator. It's mostly used with variables - e.g. a?.b?.c will result in c if both a and b are defined, and will result in undefined if either a or b is undefined/null. But it can also be used with indexing (a?.[i]) and functions (f?.(args)), of this this answer uses the second with map?.(f). :) \$\endgroup\$ Commented Jan 6, 2022 at 17:19
  • 1
    \$\begingroup\$ @KevinCruijssen That's not right; a.?b.?c will result in a.b.c if a and a.b are defined, unrelated to the variables b and c (that would be ??). ?.() is special-case syntax AFAICT \$\endgroup\$ Commented Jan 6, 2022 at 17:30
  • 1
    \$\begingroup\$ @pxeger a.?b.?c isn't valid JavaScript. I think you meant a?.b?.c? Partially my fault for that .? instead of ?. typo in the first line of my comment.. To correct & clarify part of my previous comment: ?. is the 'optional chaining' operator. It's mostly used with variables - e.g. a?.b?.c will result in a.b.c if both a and a.b are defined, and will result in undefined if either a or a.b is undefined/null. \$\endgroup\$ Commented Jan 6, 2022 at 17:54
  • \$\begingroup\$ @pxeger Anyway, it's still all the ?. operator, as can be seen in the link I send in my first comment. Whether you use it as obj?.val, arr?.[index] or func?.(args) doesn't matter too much. \$\endgroup\$ Commented Jan 6, 2022 at 17:56