Weird JavaScript scoping behavior <p>Latey I've got some trouble with some weird javascript behavior. I want to do something like this:</p> <pre><code>var lang = null; function getLang() { if (browserLanguageIsGerman) { lang = 'de'; } else { lang = 'en'; } // alert(lang) shows "de" $('#someID').load(someValidUrl, null, function(response, status, xhr) { if(languageSettingsOnFacebookIsGerman) { lang = 'de'; } else { lang = 'en'; } // alert(lang) show "en" ); // alert(lang) shows "de" } </code></pre> <p>The first and the second alerts show the expacted value 1) "de" 2) "en". The third alert shows "de" but shouldn't it be "en"?! Also the second alert pops up after the third alert.</p> <p>Can someone please obvious bug in my mind? :)</p> <p>Thanks in advance!</p>