MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/node/comments/jrau9t/conditionally_call_a_function_with_optional/gbswsc2/?context=3
r/node • u/wobsoriano • Nov 10 '20
126 comments sorted by
View all comments
58
Just because you can, doesn't mean you should
0 u/[deleted] Nov 10 '20 [deleted] 0 u/Not_Sugden Nov 10 '20 edited Nov 10 '20 it really depends on the use case for me, but i rarely run into situations where an the chaining would look more logical than chaining x && x.ylike uhconst bool = x && x.y ? x.y : false;const bool = x && x.y ? x.y === 'smth' : false;rather thanconst bool = x?.y || false;const bool = x?.y === 'smth'the 2nd example here is a better one, what use is it comparing undefined to a string, using a tenary w/ && would be more logical there no?edit: this has no relevance but i just wanted to sayyou could also doconst bool = Boolean(x) && (x.y === 'smth'); 5 u/robotslacker Nov 10 '20 The optional chaining way looks much more readable.
0
[deleted]
0 u/Not_Sugden Nov 10 '20 edited Nov 10 '20 it really depends on the use case for me, but i rarely run into situations where an the chaining would look more logical than chaining x && x.ylike uhconst bool = x && x.y ? x.y : false;const bool = x && x.y ? x.y === 'smth' : false;rather thanconst bool = x?.y || false;const bool = x?.y === 'smth'the 2nd example here is a better one, what use is it comparing undefined to a string, using a tenary w/ && would be more logical there no?edit: this has no relevance but i just wanted to sayyou could also doconst bool = Boolean(x) && (x.y === 'smth'); 5 u/robotslacker Nov 10 '20 The optional chaining way looks much more readable.
it really depends on the use case for me, but i rarely run into situations where an the chaining would look more logical than chaining x && x.y
like uh
const bool = x && x.y ? x.y : false;
const bool = x && x.y ? x.y === 'smth' : false;
rather than
const bool = x?.y || false;
const bool = x?.y === 'smth'
the 2nd example here is a better one, what use is it comparing undefined to a string, using a tenary w/ && would be more logical there no?
undefined
edit: this has no relevance but i just wanted to say
you could also do
const bool = Boolean(x) && (x.y === 'smth');
5 u/robotslacker Nov 10 '20 The optional chaining way looks much more readable.
5
The optional chaining way looks much more readable.
58
u/dudeitsmason Nov 10 '20
Just because you can, doesn't mean you should