Javascript - Object loses its value on function exit
<p>I've got this strange problem. In my code, I have a variable named <code>val1</code> which gets a value after a jQuery call, but after exiting the jQuery function it loses its value.</p>

<p>Here's the code:</p>

<pre><code>var val1;
$.getJSON('some address', null, function (result) {
    val1 = result.names[0].name;
    alert(val1); //first alert
});
alert(val1); // second alert
</code></pre>

<p>On first alert, I get the needed value, but on the second Alert - I get <code>undefined</code>.</p>

<p>Why?</p>
