Search code examples
javascriptnode.jsstringgoogle-chrome-extensionreturn-value

How to assign value to a value string in javascript


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.


Solution

  • You could use an object:

    var myObj = {};
    var q = "e";
    myObj[q] = "hio";
    
    console.log(myObj.e);