Saturday, June 30, 2007

&& es || operatorok javascriptben

Crockford video (28:20)

&& operator

  • The guard operator, aka logical and
  • If first operand is truthy
    then result is second operand
    else result is first operand
  • It can be used to avoid null references
    if (a) {
    return a.member;
    } else {
    return a;
    }
  • can be written as
    return a && a.member;

|| operator

  • The default operator, aka logical or
  • If first operand is truthy
    then result is first operand
    else result is second operand
  • It can be used to fill in default values.
    var last = input || nr_items;
  • (If input is truthy, then last is input, otherwise set last to nr_items.)