0

I don't know who to put it but in PHP, you can

<?php
  $var0 = "abc";
  $var1 = "var0";
  echo $$var1;
?>

This will output: "abc"

Can Javascript or ES6 do something like this? (also if there is a name for this, I'd be happy to know too)

Hao
  • 6,291
  • 9
  • 39
  • 88
  • I think you need to be clearer, you can do `var var1 = var0;` there's no reason to put that in additional quotes? – adeneo Oct 20 '16 at 14:23
  • You can do with `eval`, but you **don't want to**. What is [your actual problem](http://meta.stackexchange.com/q/66377)? – Bergi Oct 20 '16 at 14:23
  • JS has objects, which should do the trick, unless you're doing something really awful. – ssube Oct 20 '16 at 14:24
  • He wants to use variable variables in javascript. – Mihailo Oct 20 '16 at 14:24
  • @Mihailo - he sure does, the question is why? The answer could be anything from "no" to "template literals" – adeneo Oct 20 '16 at 14:25
  • This seems like the duplicate of [“Variable” variables in Javascript?](http://stackoverflow.com/questions/5187530/variable-variables-in-javascript) – Mihailo Oct 20 '16 at 14:26
  • Something like this : `var var0 = "abc"; var var1 = ${var0};`. [More information here](https://developers.google.com/web/updates/2015/01/ES6-Template-Strings) – Robiseb Oct 20 '16 at 14:30
  • you could do something like: var a = 'text'; var b = 'a'; console.log(window[b]), or global[b], in case of node – Taras Lukavyi Oct 20 '16 at 15:32

0 Answers0