r/node • • Nov 10 '20

Conditionally call a function with optional chaining.

Post image
345 Upvotes

126 comments sorted by

View all comments

Show parent comments

1

u/recycled_ideas Nov 10 '20

It checks if the left hand side is null or undefined and the runs the right hand side.

There's no difference between a function call and a property access.

1

u/RufusThreepwood Nov 10 '20

It checks if the left hand side is null or undefined and the runs the right hand side.

That's not an apt description of either. I'm not debating function call vs property access. The Elvis operator (?:) and optional chaining are different things.

1

u/recycled_ideas Nov 10 '20

First off, my apologies, I'd learned ?. as the Elvis operator, I come from a C# background where ?: doesn't exist.

That said, my description of optional chaining is accurate.

The reason optional chaining works on functions in JavaScript (it doesn't in most languages) is because functions in JavaScript are objects.

From the runtimes point of view accessing a property and executing involve evaluating the right hand side in terms of the left.