Double String Variable Name (Javascript) [duplicate] - javascript

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names

Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);

Related

Get variable by variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"

Accessing variable value by string name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I access local scope dynamically in javascript?
(4 answers)
Closed 3 years ago.
Issue: I want to access some variables I got in my js-file by a string.
I know there is an option to receive variable value if you have an object and pass the object key name in square brackets to the object, but this is not the case here. Here I have just one comon variable, and I want its value by "sending" a string down.
Example on what I want to achieve:
const constantName = 12345; // Value to access
const stringToUseToGetTheValueOfConstantName = 'constantName';
// I want to know how to get the '12345' by something like this:
const valueFinallyAccessed = `${stringToUseToGetTheValueOfConstantName}`; // I know this returns a string
console.log(valueFinallyAccessed); // 12345
Solution
const valueFinallyAccessed = eval(stringToUseToGetTheValueOfConstantName);
console.log(valueFinallyAccessed); // Prints out the value 12345

Is it possible to create a ES6 Javascript expression whose value is an object with a dynamic property name? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I currently do:
function outer(prop_name) {
const tmp = {};
tmp[prop_name] = 'hello world';
foo(tmp);
}
Is there a way of rewriting this as:
foo(<expression>)
using an expression involving prop_name?
You can write it as
foo({ [prop_name] : 'hello_world'});

Can I generate new array with another variable in javascript? [duplicate]

This question already has answers here:
What do square brackets around an expression mean, e.g. `var x = a + [b]`?
(7 answers)
Closed 7 years ago.
function construct(head, tail) {
return cat([head], _.toArray(tail));
}
I'd like to know the role of '[' and ']' above.
Is it an operator?
Is it the array initialize literal?
This question is answered here and on the previous QnA(Use of [square brackets] around JavaScript variables).
I felt this weird because I am not used to javascript.
But this is not a matter any more.
I thought wrong and this is explained. Thanks!
It is initializing a 1 element array to pass to cat here. Brackets can be used to access elements on objects and arrays, and to represent an array literal like you have here.

How insert variable in JS [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}

Categories

Resources