سؤال

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');
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top