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
3 Answers
Reset to default 5You 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