How to Access a JavaScript Variable Using a String That Contains the Name of the Variable

Is there a way to access a javascript variable using a string that contains the name of the variable?

Given:

var x = {
myproperty: 'my value'
};

You can access the value by:

var value = x['myproperty'];

If you're looking for a global variable, then you would check its container (window);

var value = window['x']['myproperty'];

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]);

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>

How can I refer to a variable using a string containing its name?

You can use an eval to do it, though I try to avoid that sort of thing at all costs.

alert(eval(someString));

A better way, if you find yourself needing to do this, is to use a hash table.

var stuff = { myText: 'hello world!' };
var someString = 'myText';
alert( stuff[someString] );

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 variable name into string in javascript

Use the code below.

const abc = '123';const def = '456';
const varToString = varObj => Object.keys(varObj)[0]
alert ("value of " +varToString({def}) +" is "+ def +" and value of " +varToString({abc})+" is "+abc );

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>

JavaScript object: access variable property by name as string

You don't need a function for it - simply use the bracket notation:

var side = columns['right'];

This is equal to dot notation, var side = columns.right;, except the fact that right could also come from a variable, function return value, etc., when using bracket notation.

If you NEED a function for it, here it is:

function read_prop(obj, prop) {
return obj[prop];
}

To answer some of the comments below that aren't directly related to the original question, nested objects can be referenced through multiple brackets. If you have a nested object like so:

var foo = { a: 1, b: 2, c: {x: 999, y:998, z: 997}};

you can access property x of c as follows:

var cx = foo['c']['x']

If a property is undefined, an attempt to reference it will return undefined (not null or false):

foo['c']['q'] === null
// returns false

foo['c']['q'] === false
// returns false

foo['c']['q'] === undefined
// returns true


Related Topics



Leave a reply



Submit