search

Home  >  Q&A  >  body text

A question about JavaScript

(a==b)&&(return a)
Lazy people want to use short-circuit operation, but the result is an error, syntax error, unexpected token return

黄舟黄舟2793 days ago598

reply all(3)I'll reply

  • 漂亮男人

    漂亮男人2017-05-18 10:50:39

    JavaScript logical AND operation, if the previous expression is true, then the value of the second expression is returned.

    However, the return value of the expression return a is an "Illegal return statement", that is to say, it neither returns undefined, nor null or empty value, but should not return anything at all. Therefore, an error will be reported.

    reply
    0
  • 为情所困

    为情所困2017-05-18 10:50:39

    Can return be placed in an expression?

    reply
    0
  • PHPz

    PHPz2017-05-18 10:50:39

    return a==b?a:undefined

    reply
    0
  • Cancelreply