4

Template Strings should work on any terminals such as visual studio code terminal or windows terminal. But it didn't. I did this code visual studio code. Here is My code

var name = 'Andrew';
console.log('Hello ${name}');

and the output is

Hello ${name}

Please specify changes in my code needed and explain why it doesn't work currently.

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
nsuriya239
  • 89
  • 1
  • 2
  • 11

4 Answers4

17

Single and double quotes wont invoke the behavior - use back ticks.

var name = 'Andrew';
console.log(`Hello ${name}`);
//          ^             ^

More information on Template literals,

Jaliya Udagedara
  • 1,087
  • 10
  • 16
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

All that is inside a string, is literal. You're writing the variable ${name} inside the normal quotes, so it will be printed literal. If you want to have it interpretated, you have to concatenate the answer, as for example:

console.log('Hello ' + name)

The quotes to use a template are not the ones you are using, these are the correct ones: (closed accents / back-tick )

`Hello ${name}`

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

zelda11
  • 425
  • 2
  • 7
0

var name = 'Andrew';
console.log(`Hello ${name}`);

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

Template literals are enclosed by the back-tick (``) (grave accent) character instead of double or single or double quotes.

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
0

It's not a quote, nor double quote

var name = 'Andrew'
console.log(`Hello ${name}`)

Here is a tutorial about it: https://babeljs.io/learn-es2015/#template-strings

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60