I have two questions, can anyone explain that, please?
1- Why can I access btn
outside the callback function and can't access x
outside declareX
?
2- How btn
has two different values inside and outside the eventListerner?
console.log(btn); // logs the button itself
btn.addEventListener('click', () => {
var btn = document.getElementById('btn').innerHTML;
console.log(btn); // logs the innerHTML of the button
});
(function declareX() {
var x = 0;
console.log(x); // logs 0
})();
console.log(x); // error x is not defined