0

Exactly, is console.log() a function? Why do these following two code snippets have different output?

function delay(x) {
    console.log('Start of timeout');
    return x;
};
setTimeout(function(){console.log('End of timeout');}, delay(5000));
console.log('Start to do something else');

.

function delay(x) {
    console.log('Start of timeout');
    return x;
};
setTimeout(console.log('End of timeout'), delay(5000));  // ???????
console.log('Start to do something else');
ngungo
  • 4,892
  • 4
  • 29
  • 36
  • 1
    In your first example, you pass `setTimeout` a function to be called later, but in your second example, you call `console.log` and pass the return value to the `setTimeout` function. – Carl Groner May 13 '14 at 00:09
  • 1
    the first parameter of setTimeout is a function, in the first snippet you pass the function, ok. In the second one you are calling the function, so you're not passing the function as an argument but the value returned by the function – lelloman May 13 '14 at 00:09

1 Answers1

1

Yes, console.log is a function.

The first snippet uses the expected syntac for setTimeout. The second calls console.log inline, which returns undefined. That comes the first argument to setTimeout. That explains the different timing of when End of timeout appears.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49