Just experimenting with JS. I have found something I cant get my head around.
I have this code:
prompt('title', setTimeout(() => {
return 'yes'
}, 1000));
When I first run it, it prints 2. But when I run it again, it prints 3.
But what is this counter, and why is it starting from 2? Where does the values come from? Why is it not returning yes?
When you call setTimeout the timeout is given a timeoutID (which can be later used to cancel it with clearTimeout). This timeoutID increments whenever a new timeout is made. When setting it as the default value for the prompt, it is stringified (causing the timeoutID to be returned).
The value starts from two because you've probably made a timeout before running the prompt.
Demonstration:
const first = setTimeout(() => {}, 1).toString()
const second = setTimeout(() => {}, 1).toString()
console.log(first, second)