Background
I just moved over to Javascript from Java/C/C#. I'm trying to learn by viewing some code I found online, learning by example tends to work for me the best.
I've come across a (weird and some people say) highlighting feature of JS. And it seems silly but, how do I set a value to a variable?
What I've tried
<script type="text/javascript">
let resultingJSON=9;
function selectFile() {
let selectedFile;
document.getElementById('file').click();
document.getElementById('file').addEventListener('change', function (e)
{
selectedFile = e.target.files[0];
var fileReader = new FileReader();
fileReader.readAsBinaryString(selectedFile);
fileReader.onload = (event)=>
{
let data = event.target.result;
let workbook = XLSX.read(data,{type:"binary"});
//console.log(workbook);
let workSheet = workbook.Sheets["Sheet1"];
resultingJSON = XLSX.utils.sheet_to_json(workSheet);
printJSON(resultingJSON);
Breakpoint 1
}
console.log("3--"+resultingJSON);
});
console.log("2--"+resultingJSON);
}
console.log("1--"+resultingJSON);
</script>
Observation
At breakpoint 1, the variable was set correctly. At the console logs number 1, 2, and 3, it remained 9 (its initial value).
Question
The variable was global, why was changes made to the variable not carry over globally? How can this be corrected?
console-log sequences are: 1, 2, then 3. But after the click(), addEventListener was called, so why was the sequence not 1,3,then 2 ?
Thanks guys, (references to some study material is also appreciated)