0

Consider:

(function(){
  console.log(1);
  setTimeout(function(){console.log(2)},1000);
  setTimeout(function(){console.log(3)},0);
  console.log(4);

 })();

The code written above outputs:

 1
 4
 undefined
 3
 2

What is happening behind the scene, as I can understand setTimeout function is delaying it by 1 sec and 0 sec?

Why it is printing out undefined?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shreyas D
  • 1
  • 1
  • 5
    You seem to execute that code in the browser console which will log the result of the expressions you execute from there. `undefined` is the implicit return value of `(function(){/* ... */ })();`. – str Jul 02 '18 at 08:10
  • In your code snippet it never display – NullPointer Jul 02 '18 at 08:11
  • @NullPointer I rejected your edit because the question is to do with running the code in the browser console, not the code itself. Adding a snippet would just lead to confusion as it does not behave the same as the browser console. – Reinstate Monica Cellio Jul 02 '18 at 08:18
  • @shreyas-ok got it. Thanks – NullPointer Jul 02 '18 at 08:26

0 Answers0