There are two main differences between these two forms:
setTimeout("myFunc()", 100);
and
setTimeout(myFunc, 100);
The first is less efficient and evaluates a function in the global scope, so you cannot pass it a local function or any function that is not global.
To look at the efficiency argument, if you want to call a function that you had in your code, you would write:
x = myFunc();
or write:
x = eval("myFunc()");
Of course, you should write the first, because:
- As usual you write javascript
- A function reference can be resolved once in the first pass of the interpreter, and not every time it executes
- Minifiers / optimizers can rename your character first, but not second.
- You can call local functions from the first, and the second requires a global function
eval() is a pretty heavy thing that should only be used when there is no other better way to do this.
FYI, this jsPerf comparison shows that the eval() version is 96% slower. In some cases, performance may not matter, but you can understand how less effective it is.
source share