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.

rockstarr
  • 3
  • 1
  • 1
    Does this answer your question? [How to assign multiple variables at once in JavaScript?](https://stackoverflow.com/questions/38411834/how-to-assign-multiple-variables-at-once-in-javascript) – mikemaccana Aug 19 '22 at 15:55

1 Answers1

2

You could use an object:

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

console.log(myObj.e);
James
  • 20,957
  • 5
  • 26
  • 41
  • I got this. Rather than creating an object do we have any other clean way? – rockstarr Aug 19 '22 at 16:57
  • Not as written, in your example. Do you have additional context? – James Aug 19 '22 at 17:24
  • `[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. – James Aug 19 '22 at 17:26
  • I want to take two values. One heading like "Website" and assign second value to it like "www.google.com" . – rockstarr Aug 20 '22 at 11:09
  • 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. – James Aug 20 '22 at 11:26