I ran the following code in my console:
let a3 = 100;
setTimeout( function(){
a3 = a3 + 1;
console.log(a3);
}, 4000);
console.log( ++a3+'st');
I do not understand the execution sequence of the above JavaScript Code.
I expected the output to be
EXPECTED OUTPUT
101st //since console.log(++a3+'st') executes first
101st1 //the setTimeout() function executes
But the actual output I got is
ACTUAL OUTPUT
101st
102
I want to understand that, if a3 becomes a string "101st" after console.log( ++a3+'st'); runs, then why does the code inside setTimeout()
setTimeout( function(){
a3 = a3 + 1;
console.log(a3);
}, 4000);
which runs later, give a3 as 102 and not 101st1 since "101st" + 1 = "101st1" ?
a3with 'st'. You simply increment it, string append and logs it when you doconsole.log( ++a3+'st');