You are actually setting a timeout for the return
value of console.log
, i.e., undefined
, instead of an actual function.
setTimeout
requires a function
as the first argument in order for it to work. You have passed undefined
. To fix this, there are 2 ways.
Pass a function that calls console.log
setTimeout(function () {
console.log("test");
}, 100);
In ES6, that would be much shorter:
setTimeout(() => console.log("test"), 100);
Bind arguments to console.log
setTimeout(console.log.bind(undefined, "test"), 100);
bind()
-ing arguments to a function is like telling the function to to execute with the arguments we are giving it. The function is not executed immediately when you call bind
, but the arguments are stored.
Also, as a side-note, the first argument we passed for bind
, i.e., undefined
, is the this
value for the function. We do not care about the this
value of console.log
, and we want to pass only "test"
, we write undefined
as the this
value.