Running this code in console gives an error: Cannot read property 'status' of undefined
.
xhr = new XMLHttpRequest(); // global variable
xhr.open('GET', '/');
xhr.onload = function(){
console.log(xhr.status); // this line throws the error
var xhr = 'a'; // local variable
}
xhr.send();
If I comment out the line var xhr = 'a';
, it works. Can someone explain what happens here?
Tested in Chrome and Firefox.