title: Convert string to variable name in JavaScript
body: <p>I’ve looked for solutions, but couldn’t find any that work.</p>
  
  <p>I have a variable called <code>onlyVideo</code>.</p>
  
  <p><code>"onlyVideo"</code> the string gets passed into a function. I want to set the variable <code>onlyVideo</code> inside the function as something. How can I do that?</p>
  
  <p>(There are a number of variables that could be called into the function, so I need it to work dynamically, not hard coded <code>if</code> statements.)</p>
  
  <p><em>Edit: There’s probably a better way of doing what you’re attempting to do. I asked this early on in my JavaScript adventure. Check out how JavaScript objects work.</em></p>
  
  <p>A simple intro:</p>
  
  <pre><code>// create JavaScript object
  var obj = { "key1": 1 };
  
  // assign - set "key2" to 2
  obj.key2 = 2;
  
  // read values
  obj.key1 === 1;
  obj.key2 === 2;
  
  // read values with a string, same result as above
  // but works with special characters and spaces
  // and of course variables
  obj["key1"] === 1;
  obj["key2"] === 2;
  
  // read with a variable
  var key1Str = "key1";
  obj[key1Str] === 1;
  </code></pre>