[removed] || instead of IF statement - is this legal and cross browser valid?

后端 未结 10 939
忘掉有多难
忘掉有多难 2020-12-23 20:43

It seems that:

if (typeof a == \'undefined\') {
    a = 0;
}

and

(typeof a != \'undefined\') || (a = 0)

h

10条回答
  •  一整个雨季
    2020-12-23 21:11

    (typeof a != 'undefined') || (a = 0)
    

    is an expression. But one of its subexpressions

    (a = 0)
    

    is an assignment. So essentially, this is an expression with a side effect. Statements (especially those that are expessions), that have side effects are typically one of the first things you learn not to do in an introductory coding class. So why do it simply because it only takes one line?

提交回复
热议问题