0

I have a confusion when I learn jquery source code : Please see the following the third row:

        // HANDLE: $(expr, $(...))
        } else if ( !context || context.jquery ) {
            return ( context || rootjQuery ).find( selector );

I think "rootjQuery" is "$(document)", "context" is a jquery object.

why context and rootjQuery can perform a '||' operations ? and returns the result is not boolean type ?

Thank you!

Urgent
  • 1
  • 2

1 Answers1

1

Basically, the || operator resolves to the lvalue if it is truthy and the rvalue if the lvalue is falsy. It never actually maps to true or false, unless one of its operands are true or false. So (a || b).name would be b.name if a was null, undefined, false, or whatever.

It is a relatively common practice. Like const opts = options || defaultOptions in a function where the options object may be undefined.

pfooti
  • 2,605
  • 2
  • 24
  • 37