Is it possible to call a global variable that is defined in the function from outside of the function in JavaScript? For example, I can print the selected item in the console.log that is located within the function. However, if I try to fetch the same value outside of the function, it's not working. Is there anyway to fetch the value defined within a function outside the function?
var selectedItem = '';
document.getElementById('select-option').addEventListener('change', function () {
//re-assign a new value to the variable
selectedItem = this.value;
// working
consol.log('Selected Item:', selectedItem);
});
// not working
console.log('Selected Item:', selectedItem);