Inserting if statement inside ES6 template literal

后端 未结 5 1813
慢半拍i
慢半拍i 2021-02-01 16:15

I have a simple ajax request returning some data and then inserting into a template literal. I was wondering if it it possible to insert an \'if\' statement inside the template?

5条回答
  •  鱼传尺愫
    2021-02-01 16:26

    To use a variable while using the ternary operator, use a nested template literal like this:

    let var1 = 6
    let var2 = 8
    
    console.log(`${ `${var1 > var2 ? var1 + ` (var1) `: var2 + ` (var2) `}` } is greater`)

提交回复
热议问题