views:

46

answers:

4

I know in PHP we can do something like this :

$hello = "foo";
$my_string = "I pity the $hello";

output : "I pity the foo"

I was wondering if this same thing is possible in JavaScript as well. Using variables inside strings without using concatenation -- it looks more concise and elegant to write.

+2  A: 

Nope that is not possible in javascript. You will have to resort to:

var hello = "foo";
var my_string = "I pity the " + hello;
Sarfraz
Okay, Thanks....
DMin
@DMin: You are welcome.
Sarfraz
+1  A: 

No, although you could try sprintf for JavaScript to get halfway there:

var hello = "foo";
var my_string = sprintf("I pity the %s", hello);
Justin Ethier
A: 

If you're trying to do interpolation for microtemplating, I like Mustache.js for that purpose.

Joe Martinez
+1  A: 

well you could do this, but it's not esp general

'I pity the $fool'.replace('$fool', 'fool')

You could easily write a function that does this intelligently if you really needed to

Scott Evernden