Concatenate string with ternary operator in javascript

前端 未结 2 748
再見小時候
再見小時候 2020-12-11 21:27

Its being annoying

Following code :

var arrays = [1,2,3];
alert(\'Array has \' + (arrays.length > 0) ? \'multiple\':\'single\' +         


        
2条回答
  •  天命终不由人
    2020-12-11 21:42

    Your first example is parsed this way

    alert(('Array has ' + (arrays.length > 0)) ? 'multiple':('single' + ' value'));
    

    given the operator precedence of + is much higher than that of ?:.

提交回复
热议问题