shouldn´t it be the same?
No, not at all.
setTimeout(alertFunc, 3000)
passes the value of alertFunc
(a reference to a function) into setTimeout
. setTimeout
stores that function reference in order to call it three seconds later.
setTimeout(alertFunc(), 3000)
calls alertFunc
, immediately, and passes its return value into setTimeout
. Exactly the way foo(bar())
calls bar
and passes its return value into foo
.