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

I do have a very simple use case:it('Formatting with ${}', () => {const value = 1const inf

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745375907a4624997.html

相关推荐

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

    I do have a very simple use case:it('Formatting with ${}', () => {const value = 1const inf

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信