Javascript: Reference a variable name from the variable itself
<p>I want to create a quick function that will <code>console.log</code> a variable name and the value. I'd like the result of the function to show in the console: <code>foo: bar</code>.</p>

<p>My basic idea for the function looks like this:</p>

<pre><code>function varlog(var_name)
{
    console.log(var_name + ": " + eval(var_name));
}
</code></pre>

<p>And I'd call is thusly:</p>

<pre><code>function someRandomFunction()
{
    var foo = "bar";
    // ... some stuff happens
    varlog("foo");
}
</code></pre>

<p>This works if <code>foo</code> is global, but doesn't work in the example provided. Another option that also only works globally is using <code>window[var_name]</code> instead of the scary <code>eval</code>. </p>

<p>I don't think what I'm asking is possible, but I figured I'd throw it out there.</p>

<p>I'm spending a lot of time attempting to be lazy. My current method is just <code>console.log('foo: ' + bar);</code> which works just fine. But now I just want to know if this is possible.</p>

<p>Some other questions I referenced in searching for this / creating what I have now:</p>

<ul>
<li><a href="http://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript">Variable name as a string in Javascript</a></li>
<li><a href="http://stackoverflow.com/questions/417645/how-to-convert-variable-name-to-string-in-javascript">How to convert variable name to string in JavaScript?</a></li>
<li><a href="http://stackoverflow.com/questions/1664282/javascript-refer-to-a-variable-using-a-string-containing-its-name">Javascript, refer to a variable using a string containing it&#39;s name?</a></li>
<li><a href="http://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name">How to find JavaScript variable by its name</a></li>
</ul>

<p>--</p>

<p>Edit: I'd love to just call <code>varlog(foo)</code>, if the name "foo" can be derived from the variable.</p>
