The example you posted makes use of a few features of the JavaScript language:
It's semantically equivalent to this:
var array = undefined;
if( props /* is not null or undefined or empty-string */ ) {
array = props.children.find( ele => ele && ele.length );
}
(Note the additional &&
inside the find
predicate, so in-full it becomes this):
var array = undefined;
if( props /* is not null or undefined or empty-string */ ) {
array = props.children.find( function( ele ) {
if( ele /* is not null or undefined or empty-string */ ) {
return ele.length;
}
return undefined;
} );
}
It can also be compared to the "Elvis operator" aka Safe-navigation operator from C#:
var array = props?.children.find( e => e?.length );
Explanation:
The &&
operator evaluates its left operand first, in this case just props
- if it is not falsy (not null, undefined or an empty string) then it evaluates the right operand (in this case, the props.children.find
function invocation). Note that an empty array is not falsy.
If props
were falsy, then the .children.find
call would not be made, preventing a runtime error.