This is a common way to make sure your function exists before you call it.
It works like this (From developer.mozilla.com):
expr1 && expr2
Returnsexpr1
if it can be converted tofalse
; otherwise, returnsexpr2
. Thus, when used with Boolean values,&&
returnstrue
if both operands aretrue
; otherwise, returnsfalse
.
In other words, Javascript does not coerce the operands to boolean values unless it has to.
4 && 5
Returns 5, not true.
In your case, if the first expression is undefined
(which is convertible to false), then ctx
will be false, and the second expression does not get evaluated. If the first expression is a function (which cannot be converted to false
), then Javascript evaluates the second expression, and assigns it’s value to the ctx
variable.