Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript parse variable in quote [duplicate]

Tags:

javascript

Possible Duplicate:
JavaScript Variable inside string without concatenation - like PHP

In PHP, double quotes has the ability to read variable, e.g.

"$foo"

But in JavaScript, you have to always use a + to read a variable so that the variable won't be inside the quote when it is read, e.g.

var foo='bar';
alert("The name's "+foo);

So, is there any workaround or method to do this? Using + all the time is quite troublesome.

like image 980
siaooo Avatar asked Jul 01 '26 23:07

siaooo


1 Answers

Nope, that's not possible in JavaScript.

In JavaScript, variables are turned into string when put in single quotes or doubles quotes and can't be parsed. In JavaScript everything inside quotes is treated as string.

Even if you write custom parser, you will have no way to figure out if something in quotes is really a variable or an string because a variable named name can also appear in string somewhere which will create naming collisions.

like image 190
Blaster Avatar answered Jul 04 '26 14:07

Blaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!