I had encountered something unclear to me in the for loop:
var info = {
"full_name" : "John Doe",
"title" : "FrontEnd",
"links" : {
"blog" : "JohnDoe.com",
"facebook" : "http://facebook.com/JohnDoe",
"youtube" : "http://www.youtube.com/JohnDoe",
"twitter" : "http://twitter.com/JohnDoe"
}
};
I'm looping through this object with this loop:
var output = "";
for (var key in info.links ) {
if (info.links.hasOwnProperty(key)) {
output += '<li>' +
'<a href = "' + info.links[key] +
'">' + key + '</a>' +
'</li>';
console.log(key);
}
}
var update = document.getElementById('links');
update.innerHTML = output;
And my question is, what is var key in this loop and why it works when there is no var key in scope of this loop? In this case var key becomes blog, facebook etc. inside info.links object. But why?