最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

string - ${value} in JavaScript doesn't work - Stack Overflow

programmeradmin0浏览0评论

I do have a very simple use case:

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

   assert.equal(info, 'the result is: 1')
})

But it doesn't work and I can't see why. It's using ES6 because arrow function works. I tried let instead of const, even var. Nothing works.

Can anybody help?

Best regards, Torsten

I do have a very simple use case:

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

   assert.equal(info, 'the result is: 1')
})

But it doesn't work and I can't see why. It's using ES6 because arrow function works. I tried let instead of const, even var. Nothing works.

Can anybody help?

Best regards, Torsten

Share Improve this question asked Feb 2, 2018 at 7:01 zimmyblnzimmybln 1513 silver badges15 bronze badges 1
  • 1 Change the single quote to backticks for info variable. – Tushar Commented Feb 2, 2018 at 7:04
Add a ment  | 

3 Answers 3

Reset to default 5

You have to use back-ticks (`) instead of single quote (') or double quote (") to use string interpolation

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

    assert.equal(info, 'the result is: 1')
})

Do

`the result is: ${value}`

instead of 'the result is: ${value}' .

This happens because in JavaScript there's a concept of Template literals which let's user evaluate embedded expressions. You can read more about it in the link provided.

Instead of 'result is: ${value}'

Please use Backticks

const test = 'hello!'


// Template Literal
console.log(`testing: ${test}`)

If you dont know where Is the Backtick is Mostly near the Esc button

Hopes this help

发布评论

评论列表(0)

  1. 暂无评论