How to Find JavaScript Variable by Its Name

How to find JavaScript variable by its name

<script>
var a ="test";
alert(a);
alert(window["a"]);
alert(eval("a"));
</script>

Variable name as a string in Javascript

Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.

var obj = { myFirstName: 'John' };obj.foo = 'Another name';for(key in obj)    console.log(key + ': ' + obj[key]);

JavaScript: How to get value of variable by name

Try this:

var text1 = "There is text1";
var text2 = "There is text2";
var text3 = "There is text3";

actual_text = "text2";

document.getElementById('myoutput').innerHTML = this[actual_text];
<p id="myoutput"></p>

Access value of JavaScript variable by name?

Global variables are defined on the window object, so you can use:

var MyVariable = "Value of variable";
alert(window["MyVariable"]);

Select a variable/object by it's name from many available with Javascript

JavaScript won't give you a list of declared variables and their names, so what you're trying to do won't work with plain variables unless you use eval like in Majed's answer, but I don't recommend that—using eval is generally discouraged because depending on your code it can open you up to security vulnerabilities.

What you could do instead is store JohnA and GeorgeA as properties on an object like so:

let names = {
JohnA: { countries: ... },
GeorgeA: { countries: ... }
}

and then you can programmatically access those properties:

let name = 'John';
names[name + 'A'].countries // ...

Javascript: Reference a variable name from the variable itself

The reason it doesn't work is because the variable foo is not accessable to the function varlog! foo is declared in someRandomFunction, and is never passed into varlog, so varlog has no idea what the variable foo is! You can solve this problem by passing the variable foo into the function(or using some sort of closure to make foo in the scope of varlog) along with its string representation, but otherwise, I think you are out of luck.

Hope this helps.

Get global variable dynamically by name string in JavaScript

Do you want to do something like this?

<script>
//in one script
var someVarName_10 = 20;

alert(window["someVarName_10"]); //alert 20

</script>

Update: because OP edited the question.

<script>
num=10;
alert(window['someVar' + 'Name_' + num]); //alert 20
</script>

javascript : get a global variable from its name as string (reflection)

you could do:

var hello = 'Hello World';
this['hello'];

I wouldn't make those variables so global though. here's why
Instead put them inside an object like:

var obj = {
a: 'Hello',
b: 'World'
}
console.log(obj['a'], obj['b']);

Get variable names with JavaScript

so the argument is an array of variables? then no, there is no way to get the original variable name once it is passed that way. in the receiving end, they just look like:

["123","abc"];

and nothing more


you could provide the function the names of the variables and the scope they are in, like:

function log(arr,scope){
for(var i=0;i<arr.length;i++){
console.log(arr[i]+':'scope[arr[i]]);
}
}

however, this runs into the problem if you can give the scope also. there are a lot of issues of what this is in certain areas of code:

  • for nonstrict functions, this is window
  • for strict functions, this is undefined
  • for constructor functions, this is the constructed object
  • within an object literal, this is the immediate enclosing object

so you can't rely on passing this as a scope. unless you can provide the scope, this is another dead end.


if you pass them as an object, then you can iterate through the object and its "keys" and not the original variable names. however, this is more damage than cure in this case.

How find variable/array if I don't know full variable name in javascript

Since it's declared with var on the top level, you can iterate over the properties of the window and find one which startsWith what you're looking for:

// Example site code:var array_636353 = [62, 96, 11, 28];
// Userscript code:const prop = Object.keys(window).find(key => key.startsWith('array_'));console.log(prop, window[prop]);


Related Topics



Leave a reply



Submit