0

I am executing the following code snippet on browser console.

console.log(
setTimeout(function(){
    console.log('a');
},200));

This gives me two outputs. First output is a random number(that is what i thought) and second is a. While I understand that the second output is normal, what is the first random number that is being generated in console. enter image description here

Shouvik
  • 449
  • 1
  • 7
  • 17

2 Answers2

4

The other number that is being logged is the return value of setTimeout function, which is the id for the timer, which can be used to clear the timer. See this for details

Varun Sharma
  • 1,602
  • 1
  • 13
  • 37
2

From MDN:

The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Return_value

The 8 you are seeing is the return value of the call to setTimeout, which is passed to console.log and output.

linuxuser
  • 121
  • 7