Ternary operator with multiple operations

前端 未结 3 1614
星月不相逢
星月不相逢 2021-01-24 02:11

Can I use a ternary operator when I have more than one operation to perform per case?

For example can I use it here?:

    if (dwelling) {
        dwellin         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 02:36

    Yes, see: Conditional (ternary) Operator

    var dwelling = true;
    (dwelling) ? (
        dwelling = 'a',      //first operation
        letterDwelling = 'a' //second operation
    ) : (
        dwelling = 'b',
        letterDwelling = 'b'
    );
    alert(dwelling);
    

    jsfiddle example

提交回复
热议问题