How to create var name with multiple var using javascript?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

How to create var name with multiple var using javascript?



I want to create create var name a_test_3_check from multiple var and text using javascript For this code , i want to alert test


a_test_3_check


test



I tried to do with this code but not work. how can i do ?


<script>
var a_test_3_check = "test";

var x = "test";
var y = "check";

var z = 'a_'+x+ '_3_' +y;
alert(z);
</script>



And when i test code i alway alert a_test_3_check, how can i do ?


a_test_3_check



Thank you for every question.





Just getting my comment in before someone posts an answer using eval(): if you run into a situation where you have to dynamically construct and evaluate variable names, there is most likely something wrong with your design. Explain what you're trying to achieve, and we can help you think of alternatives.
– Robby Cornelissen
30 mins ago



eval()





@RobbyCornelissen To expand on this there is never a reason why you would need to do this. The name of a variable should have not have an impact on the functionality of your code.
– Spencer Wieczorek
24 mins ago






@SpencerWieczorek We are in complete agreement. Yet, there are already two eval() answers and they are even getting upvoted.
– Robby Cornelissen
23 mins ago



eval()





Don't do that. You should not generate variable names dynamically. At the most, you should create a JavaScript object and then get the value, based on its key, which is perfectly fine. Read working with objects.
– 31piy
23 mins ago





2 Answers
2



You can use eval(), If the argument to this function represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements


eval()


eval()


eval()



var z = eval('a_'+x+ '_3_' +y);


var z = eval('a_'+x+ '_3_' +y);



If you try with eval() then you will get what you want.


eval()


var a_test_3_check = "test";

var x = "test";
var y = "check";

var z = 'a_'+x+ '_3_' +y;
alert(eval(z));



BUT, YOU SHOULD NOT USE eval() coz it not the proper way to get variable of variable, try using object structure though.


eval()




var obj =
x: "test",
y: "check",
a_test_3_check: "test"
;

var z = 'a_' + obj.x + '_3_' + obj.y;
console.log(obj[z]);





down voter, see my edit.. I wast writing the code, in the meantime you just down voted twice.Didn't wait for my full answer :(
– Being Sunny
17 mins ago





1) Nobody can downvote twice 2) If you want people to wait for your complete answer before they vote, don't post before you have a complete answer.
– Robby Cornelissen
12 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs