0
in the end we get value of x equal 3. Why it’s not 2?
let x = 1; setTimeout( () => { x = 2; }, 1000); x = 3;
2 Antworten
+ 4
The code will run like these steps:
1- x = 1
2- you set a 1000ms or 1 second timeout
3- x = 3
4- console.log(x) for showing x value
And after 1 second:
5- x = 2
setTimeout is an asynchronous function in JavaScript, so it will not stop the other statements below it to run!
If you want to get 2 back, you need to call console.log(x) like this in your code ->
setTimeout(() => {
x = 2;
console.log(x);
}, 1000);
+ 3
Post the link to your code so that we can view the problem to just in to help you.