template strings not working

10.9k Views Asked by At

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.

4

There are 4 best solutions below

0
On BEST ANSWER

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

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

More information on Template literals,

0
On

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.

0
On

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

0
On

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