I'm having a problem. when I declare a variable in the global area and then change it in a function, Since the javascript engine first read all of the code and then goes back and run it line by line, when I come to use the variable again after I changed it, it has the same value as it has in the start.
this is an example:
var num = 2;
document.getElementById("button").onclick(function(){
num = 3;
});
console.log(num); // will log 2
if(num == 3){
window.alert('hi!');// will never do the alert
}
If I click the element with the #button id, it might change the variable num, but as I said - the javascript engine first reads all of the code and then execute it line bu line, so the result in the console.log will be 2, and not 3. the same goes for the condition part - it will never be true because in the end, num = 2 and not 3.
does anybody knows what can I do in order to use the updated value of the variable?