Using a function in a variable
<p>I have a global function that I've set and tested to make sure it's working:</p>

<pre><code>function shortenUrl(url) { 
   ...
}
</code></pre>

<p>Then I'm trying to call that function in a variable in another function like this: </p>

<pre><code>var pageLink = $(location).attr('href'),
    shortLink = shortenUrl(pageLink);
</code></pre>

<p>So when I try to run this I get an undefined error. However if I change the the pageLink variable from finding the page url with jQuery to something hard coded like "http://example.com" and console.log the value in the global function I'll get a shorten linked returned. </p>

<p>I'm just commenting out the return call like this:</p>

<pre><code>//return shortUrl;
console.log(shortUrl);
</code></pre>

<p>That's giving me the correct value in the console. But it's not getting returned back to my shortLink variable because that still shows up as undefined. Am I missing something simple here? Why isn't the value returning to the shortLink variable and why does the link that's dynamically created with jQuery pass through the function? </p>

<p>UPDATE edit:</p>

<p>Okay to reset this just a little bit. I think the problem lays within the shortenUrl function or how I'm passing values to it. Just to test the function I did this:</p>

<pre><code>    function shortenUrl(url) {

        var url = url,
            username = 'username',
            key = 'api key';

        $.ajax({
            url: 'http://api.bit.ly/v3/shorten',
            data: {
                longUrl: url,
                apiKey: key,
                login: username
            },
            dataType: 'jsonp',
            success: function (v) {
                var shortUrl = v.data.url;
                return shortUrl;
                //console.log(shortUrl);
            }
        });
    }

var shorten = shortenUrl('http://google.com');
console.log(shorten);
</code></pre>

<p>Console.log(shorten) returns undefined. However if I uncomment the console.log(shortUrl); and comment out return shortUrl; within the function I get a correctly shorten URL returned from bit.ly.</p>

<p>Why wouldn't I get the correct shortened url returned at the console.log(shorten)? Is the variable being passed wrong?</p>
