prabhakar Reddy G
prabhakar Reddy G

Reputation: 1069

Variable is not replaced by its value

am kind of novice to nodeJS and javascript, while trying to print a variable value, the variable isn't getting replaced by its value.

var a="World";
console.log("Hello ${a}");

am getting "Hello ${a}" instead of "Hello World"

Upvotes: 0

Views: 453

Answers (3)

punith bp
punith bp

Reputation: 174

you can use this , you need to put assigned variable(a) out of string "" or else it will considered as a string

   var a="World";
console.log("Hello",a);
 

Upvotes: 0

Antonio Val
Antonio Val

Reputation: 3340

If you are using ES6 you can use template literals with placeholders (which you are trying to do it seems):

var a="World";
console.log(`Hello ${a}`);

Other option is the traditional way, concatenating strings with the + operator:

var a="World";
console.log("Hello " + a);

Upvotes: 1

Suren Srapyan
Suren Srapyan

Reputation: 68635

You need to use `` instead of "" . For more see Interpolation in Javascript

var a="World";
console.log(`Hello ${a}`);

Upvotes: 2

Related Questions