JavaScript 中的 ${value} 不起作用

zim*_*bln 0 javascript string formatting

我确实有一个非常简单的用例:

it('Formatting with ${}', () => {
   const value = 1
   const info = 'the result is: ${value}'

   assert.equal(info, 'the result is: 1')
})
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我不明白为什么。它使用 ES6 因为箭头函数可以工作。我尝试用 let 代替 const,甚至用 var 代替。什么都不起作用。

有人可以帮忙吗?

最好的问候,托斯顿

Chi*_*dra 5

您必须使用反引号 ( `) 而不是单引号 ( ') 或双引号 ( ") 才能使用字符串插值

it('Formatting with ${}', () => {
    const value = 1
    const info = `the result is: ${value}`

    assert.equal(info, 'the result is: 1')
})
Run Code Online (Sandbox Code Playgroud)