Strange "Undefined variable" in a JavaScript piece of code
<p>I'm a c++ programmer so the following undefined variable error in java script is strange for me. I've defined a global variable ,<code>directionResult</code> and the following code initializes its value :</p>

<pre><code>function calcRoute() {
    var iMap = {
        departure:"tiran,esfahan",
        destination:"esfahan"
    }
    var request = {
        origin : iMap.departure,
        destination : iMap.destination,
        travelMode : google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
            document.getElementById("log").innerHTML = result.routes[0];
            directionResult = result;
        }
    });
}
</code></pre>

<p>in the last if <code>directionResult</code> will be equal to <code>result</code> which is a parameter to a call-back function. But in another function :</p>

<pre><code>function showSteps() {
    var myRoute = directionResult.routes[0].legs[0];
    var point = myRoute.steps[index].start_point;
    var inst = myRoute.steps[index++].instructions;
    obj.setPosition(point);
    document.getElementById('inst').innerHTML = inst;
    map.panTo(point);
    if (index &gt;= myRoute.steps.length)
        clearInterval(timer);
} 
</code></pre>

<p>when I want to use <code>directionResult</code> at the first line,I encounter this error:</p>

<blockquote>
  <p>TypeError: directionResult is undefined</p>
</blockquote>

<p>How can I solve this strange behavior?thanks.</p>
