0

I want to assign value to a value string. Sample as below written in javascript

var q="e"; [q]='hio';

e should have value of 'hio'

JavaScript: Assign a value to a string

the above link solves the problem. But using eval is not right approach. So I am looking for better approach.

1

1 Answer 1

2

You could use an object:

var myObj = {};
var q = "e";
myObj[q] = "hio";

console.log(myObj.e);

Sign up to request clarification or add additional context in comments.

5 Comments

I got this. Rather than creating an object do we have any other clean way?
Not as written, in your example. Do you have additional context?
[q]='hio' - has a specific meaning already in Javascript. It means, assume the right hand side is an array, take the first element, and store it in q.
I want to take two values. One heading like "Website" and assign second value to it like "www.google.com" .
That’s commonly called a “key/value pair”, where the key is website and the value is google. Search for “store key value pair in javascript” if you’d like to get some other ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.