Jump to content

So, I recently discovered that you can generate new variables in PHP by doing something like

${"randomString"} = true;

//or something like this:

$counter = 0;

while($counter < 10)
{
	${"var_" . $counter} = "This is a variable with a by PHP generated name";
	$counter = $counter + 1;
}

This allows me to generate new variables with custom names. Is there something similar in Javascript?

Link to comment
https://linustechtips.com/topic/565474-javascript-equivalent-of-php-randomname/
Share on other sites

Link to post
Share on other sites

can't you just like

var randomString = 5;


???

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to post
Share on other sites

14 minutes ago, DXMember said:

can't you just like

 


var randomString = 5;


 

???

Yes, of course I can. But using a thing called 'variable variables' you can generate variables, with names, generated by code. Look at the link if you want to know more. I'm not very good at explaining this. :)

Link to post
Share on other sites

If it's a global variable, you can access it using 

window[randomName], but if it's a local variable, I think you have to use eval if you want to use an arbitrary name, unless you just add them to an object instead. 

HTTP/2 203

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×