Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  23] [ 3]  / answers: 1 / hits: 15947  / 12 Years ago, thu, june 28, 2012, 12:00:00

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.


More From » javascript

 Answers
18

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.


[#84600] Wednesday, June 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;