return String vs Integer vs undefined vs null

后端 未结 6 1773
醉话见心
醉话见心 2021-02-15 10:50

Why does javascript prefers to return a String over any other choices ?

Consider the following snippet.

var arr = [\'Hello1\', \'Hello2\', \         


        
6条回答
  •  深忆病人
    2021-02-15 11:00

    The statement

    return this[x] || null || 'aïe' || 12 || undefined ;
    

    will evaluate the subexpressions left to right, and will return the first subexpression that does not evaluate to false. The non-existent element evaluates to false as it's undefined, and null is false by definition. That leaves the string as the first non-false subexpression, so that's what you get.

提交回复
热议问题